none
Unable to email Mail Merge Word document as HTML in C#? RRS feed

  • Question

  • Hi I'm trying to automate Microsoft Word to email Mail Merge document from Visual C# 

            Word.Application wrdApp;
            Word._Document wrdDoc;
            Object oMissing = System.Reflection.Missing.Value;
            Object oFalse = false;
            Object oTrue = true;
    
            private void InsertLines(int LineNum)
            {
                int iCount;            	
                for (iCount = 1; iCount <= LineNum; iCount++)
                {
                    wrdApp.Selection.TypeParagraph();
                }
            }
    
            private void FillRow(Word._Document oDoc, int Row, string Text1, string Text2)
            {            
                oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(Text1);
                oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(Text2);
                
            }
    
    
            private void CreateMailMergeDataFile()
            {
                Word._Document oDataDoc;
                int iCount;
    
                Object oName = "D:\\ZDoc.doc";
                Object oHeader = "FirstName, EmailAddress";
                wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing, ref oMissing, ref oHeader, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
                
                oDataDoc = wrdApp.Documents.Open(ref oName, 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 );
    
                for (iCount = 1; iCount <= 2; iCount++)
                {
                    oDataDoc.Tables[1].Rows.Add(ref oMissing);
                }
                
                FillRow(oDataDoc, 2, "Steve", "xxxxxxx@xxx.com");
                FillRow(oDataDoc, 3, "Johny", "xxxxxxx@xxx.com");
                FillRow(oDataDoc, 4, "Brian", "xxxxxxx@xxx.com");
                
                oDataDoc.Save();
                oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);
            }
    
            private void button1_Click(object sender, System.EventArgs e)
            {
                Word.Selection wrdSelection;
                Word.MailMerge wrdMailMerge;
                Word.MailMergeFields wrdMergeFields;
                Word.Table wrdTable;
                string StrToAdd;
                
                wrdApp = new Word.Application();
                wrdApp.Visible = true;
                
                wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                wrdDoc.Select();
    
                wrdSelection = wrdApp.Selection;
                wrdMailMerge = wrdDoc.MailMerge;
                
                CreateMailMergeDataFile();
                
                wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                wrdMergeFields = wrdMailMerge.Fields;            
                
                wrdSelection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
    
                wrdSelection.TypeText("Dear ");
                wrdMergeFields.Add(wrdSelection.Range, "FirstName");
                wrdSelection.TypeText(",");
                InsertLines(2);
                
                StrToAdd = "Thank you for your interest in ........................................................................";
                wrdSelection.TypeText(StrToAdd);
    
                InsertLines(2);
                StrToAdd = "Thanks,\r\n\r\nXXXXXXX.";            
    
                // Perform mail merge.
                wrdDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToEmail;
                wrdDoc.MailMerge.MailAddressFieldName = "EmailAddress";
                wrdMailMerge.Execute(ref oTrue);
    
                // Close the original form document.
                wrdDoc.Saved = true;
                wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
    
                // Release References.
                wrdSelection = null;
                wrdMailMerge = null;
                wrdMergeFields = null;
                wrdDoc = null;
                wrdApp = null;            
                System.IO.File.Delete("D:\\ZDoc.doc");
            }
    the word document was produced perfectly i can see the merge fields in word document. But emails are not being sent. Can someone help me where i'm doing wrong here.

    Monday, September 1, 2014 3:19 AM

Answers

All replies