There are a few articles on the Internet that show how to automate the mail merge, including this one from Microsoft. However, most of the ones that I found also include code to create a data source and a form letter from scratch. If you already have the data, there's no reason re-populate it into a new data document - only to have to delete it when you done. And you certainly don't want to maintain form letter content from your code, either.

This article shows how to automate a Microsoft Word mail merge with an existing form letter (not generated from code), and the ability to connect to an existing data source without having to recreate it each time. Here are the steps:

The Form Letter

The document that is used to generate the letters (the form letter) should be setup the same way you always would in Microsoft Word 2003. However, when you setup the data source, go ahead and save it so that you ODC file it generates later. Go ahead and test the Mail Merge to make sure it works.

The Data Source File

The ODC file that you saved is under My Documents\My Data Sources. The advantage of having this file is that you can change the file when the database connection changes (i.e. when you move your application from development to production.)

The Application

This article uses a Microsoft Visual Studio 2005 solution in C# to build the windows application that performs the automatic mail merge. You'll need to add a reference to Microsoft Word 11.0 Object Library in order for this to work. Once you do that, here's the code you need to get it done.


Microsoft.Office.Interop.Word.ApplicationClass wordApp = new
  Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
string rootpath = Application.StartupPath;
object vkMissing = System.Reflection.Missing.Value;
object vkReadonly = false;
object vkVisible = true;
object vkFalse = false;
object vkTrue = true;
object vkDynamic = 2;
object vkFileToOpen = (object)rootpath + "\\form_letter.doc";
wordApp.Visible = true;

try
{
  wordDoc = wordApp.Documents.Open(ref vkFileToOpen, ref vkMissing, ref vkReadonly,
  ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing,
  ref vkMissing, ref vkMissing, ref vkVisible, ref vkMissing, ref vkMissing, ref   vkMissing,
  ref vkMissing, ref vkMissing);

  wordDoc.Select();
  wordDoc.MailMerge.OpenDataSource(rootpath + "\\conn.odc", ref vkMissing,
  ref vkTrue, ref vkFalse, ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing,
  ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing, ref vkMissing,
  ref vkMissing, ref vkMissing);

  wordDoc.MailMerge.Destination =
  Microsoft.Office.Interop.Word.WdMailMergeDestination.wdSendToNewDocument;
  wordDoc.MailMerge.Execute(ref vkFalse);
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}
finally
{
  wordDoc.Close(ref vkMissing, ref vkMissing, ref vkMissing);
}

Here's a sample project that uses this code. This post is from my other blog and wanted to make sure it was included in this one so that I don't lose it. I have not tested this with Microsoft Word 2007; when I do, I'll give you the update. Good luck with it.

June 5, 2008 16:09 by RafaelV
E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Comments