HI, I am trying to send the mailmerge document as email.I am novice in this topic .could you please let me what went wrong and its correction to get the required result.
Title.doc is my template and am using excel as datasource.I am trying to send email to mailmerge doc to emailid mentioned in excelsheet.
private void GenerateMultipleDocumentAndSendEmail()
{
Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
object oNotTrue = false;
object oFilename = @"D:\Title.doc";
myWordApp.Visible = false;
string constring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + txtBrowse.Text + "; Extended Properties=Excel 12.0 Xml";
OleDbConnection con = new OleDbConnection(constring);
try
{
int iCount = 0;
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] excelsheets = new string[dt.Rows.Count];
foreach (System.Data.DataRow dr in dt.Rows)
{
excelsheets[iCount] = dr["TABLE_NAME"].ToString();
iCount++;
}
for (int jCount = 0; jCount < excelsheets.Length; jCount++)
{
OleDbCommand cmd = new OleDbCommand("Select * from [" + excelsheets[jCount] + "]");
cmd.Connection = con;
try
{
OleDbDataReader reader = cmd.ExecuteReader();
int i = 1;
while (reader.Read())
{
Microsoft.Office.Interop.Word.Document mydoc = myWordApp.Documents.Add();
object destination = @"D:\NewDocument" + i.ToString() + ".doc";
System.Collections.Generic.Dictionary<string, string> row = new System.Collections.Generic.Dictionary<string, string>();
row.Add("Title", reader.GetString(0).ToString());
row.Add("FirstName", reader.GetString(1).ToString());
row.Add("MiddleName", reader.GetString(2).ToString());
row.Add("LastName", reader.GetString(3).ToString());
row.Add("Suffix", reader.GetString(4).ToString());
row.Add("Company", reader.GetString(5).ToString());
row.Add("EmailAddress", reader.GetString(6).ToString());
mydoc = myWordApp.Documents.Add(ref oFilename, ref oMissing, ref oMissing, ref oMissing);
foreach (Word.MailMergeField myField in mydoc.MailMerge.Fields)
{
myField.Select();
string key = myWordApp.Selection.Text;
key = key.Replace("»", "").Replace("«", "");
if (row.ContainsKey(key))
{
string text = row[key];
myWordApp.Selection.TypeText(text);
}
}
mydoc.SaveAs(ref destination, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
mydoc.Close(ref oNotTrue, ref oMissing, ref oMissing);
myWordApp.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail;
myWordApp.ActiveDocument.MailMerge.MailAsAttachment = false;
myWordApp.ActiveDocument.MailMerge.MailSubject = "Hi welcome to Test1";
myWordApp.ActiveDocument.MailMerge.MailAddressFieldName = "EmailAddress";
myWordApp.ActiveDocument.MailMerge.Execute(ref oNotTrue);
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
myWordApp.Application.Quit(ref oNotTrue, ref oMissing, ref oMissing);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}