Click here to Skip to main content
15,887,876 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
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.

C#
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"; // word template
            myWordApp.Visible = false;
            //Create connection object
            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;
                    //cmd.CommandText=CommandBindings
                    try
                    {
                        OleDbDataReader reader = cmd.ExecuteReader();
                        int i = 1;
                        while (reader.Read())
                        {
                            Microsoft.Office.Interop.Word.Document mydoc = myWordApp.Documents.Add();//new Microsoft.Office.Interop.Word.Document();//
                            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);
                           //mydoc = myWordApp.Documents.Open(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);
                            myWordApp.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; ///I get error at this line
                          //  mydoc.MailMerge.MailFormat = Word.WdMailMergeMailFormat.wdMailFormatPlainText;
                            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++;
                        }//end while
                        //reader.Close();
                    }//try end
                    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);
            }

        }
Posted
Updated 31-Jul-13 22:04pm
v3
Comments
ZurdoDev 1-Aug-13 9:11am    
The error is clear but what line of code is causing it and what is your question?
keerth516 1-Aug-13 13:56pm    
myWordApp.ActiveDocument.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail; ///I get error at this line

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900