Link to home
Start Free TrialLog in
Avatar of Philluminati
Philluminati

asked on

Automating an MS Word Mail Merge using C#


Below is a sniper of code from an application I'm writing in C#. I have added the Microsoft Office Word 11.0 Object Library and the TEMPLATE_FILENAME constant has a valid filename. However the code hangs on the statement



           //Open Microsoft Word
            Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();
            wordapp.Visible = false;

            object filename = TEMPLATE_FILENAME;
            object objTrue = true;
            object objFalse = false;
            object objMiss = Type.Missing;
            Microsoft.Office.Interop.Word.Document myMergeDocument;

            myMergeDocument = wordapp.Documents.Open(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
            myMergeDocument.Select();

           
            //Microsoft.Office.Interop.Word.MailMerge mailMerge = myMergeDocument.MailMerge;

            // ON THE LINE BELOW: "COMException was unhandled." and "requested object is not available"
            myMergeDocument.MailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
            myMergeDocument.MailMerge.SuppressBlankLines = true;
            myMergeDocument.MailMerge.DataSource.FirstRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
            myMergeDocument.MailMerge.DataSource.LastRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
            myMergeDocument.MailMerge.Execute(ref objFalse);
            //myMergeDocument.Close();
                       
            wordapp.Visible = true;
            wordapp.ShowMe();
            return;


--------------

I have a word document that is set up with a mail merge source already and I just want to make it open and perform the merge. The merge source is a text file. The Open command doesn't seem to work correctly because the mail merge toolbar is disabled. Someone please help!!!


(I know how to use try catch blocks - i'm interested in fixing the problem...not just handling it gracely)
Avatar of Philluminati
Philluminati

ASKER


I've solved my own question:

When the document is opened, the data source isn't opened (and just like the "Merge to Printer" toolbar option isn't available until you a merge source, so to, the code doesn't work until the data source has been opened.)

It's opened like this:

            //Open the data source
            object source = FILE_NAME;
            object format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatText;
            myMergeDocument.MailMerge.OpenDataSource(FILE_NAME, ref format, ref objFalse, ref objMiss, ref objTrue, ref objFalse,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss);

-------------------------

Now my code looks like this:

            //Open Microsoft Word
            Microsoft.Office.Interop.Word.ApplicationClass wordapp = new Microsoft.Office.Interop.Word.ApplicationClass();
            wordapp.Visible = false;

            object filename = TEMPLATE_FILENAME;
            object objTrue = true;
            object objFalse = false;
            object objMiss = Type.Missing;
            Microsoft.Office.Interop.Word.Document myMergeDocument;

            //Open the Template file
            myMergeDocument = wordapp.Documents.Open(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
            myMergeDocument.Select();

            //Open the data source
            object format = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatText;
            myMergeDocument.MailMerge.OpenDataSource(FILE_NAME, ref format, ref objFalse, ref objMiss, ref objTrue, ref objFalse,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss,ref objMiss);
           
            //Perform the Mail Merge!!!

            if (DIRECT_PRINT)
                myMergeDocument.MailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToPrinter;
            else
                myMergeDocument.MailMerge.Destination = Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;

            myMergeDocument.MailMerge.SuppressBlankLines = true;
            myMergeDocument.MailMerge.DataSource.FirstRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
            myMergeDocument.MailMerge.DataSource.LastRecord = (int)Microsoft.Office.Interop.Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
            myMergeDocument.MailMerge.Execute(ref objFalse);

           
           
            //Close the template document.
            myMergeDocument.Close();

            //Print the derived document.
                       
            wordapp.Visible = true;
            wordapp.ShowMe();
            return;

...and it works.

I've solved my own problem.

Phill
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have the same issue.