Microsoft KB Archive/278260

= HOWTO: Use Microsoft Foundation Classes (MFC) to Automate Word and Create a Mail Merge for Mailing Labels =

Article ID: 278260

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Word 2000 Standard Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Foundation Class Library 4.2

-



This article was previously published under Q278260



SUMMARY
This article describes how to use Microsoft Foundation Classes (MFC) to automate Microsoft Word to create and execute a mail merge for mailing labels.



MORE INFORMATION
The sample code in this article uses a tab-delimited text file (C:\Data.txt) as the data source. The text file has five fields: Contact_Name, Address, City, Postal_Code, and Country. You can use any text editor to create the text file data source. You can also use any other data source, such as a Microsoft Access database, instead of the tab-delimited text file.

When you create the data source, remember to separate the fields (columns) with tab characters and separate the records (rows) with carriage returns. For example:

Contact_Name      Address                City       Postal_Code   Country Maria Anders      Obere Str. 57         Berlin     12209         Germany Thomas Hardy      120 Hanover Sq.        London     WA1 1DP       UK Hanna Moos         Forsterstr. 57        Mannheim   68306         Germany Laurence Lebihan  12, rue des Bouchers   Marseille  13008         France

Step-by-Step Sample
 Create a new dialog-based MFC AppWizard EXE project, and name it AutoWord. On the View menu, click ClassWizard (or press CTRL+W). On the Automation tab, click Add Class, and then click From a Type Library. Browse to the Word type library (MSWord8.olb for Word 97 or MSWord9.olb for Word 2000).NOTE: The type library is located in the same folder as Winword.exe, which is installed by default at C:\Program Files\Microsoft Office\Office.

 Add all of the classes in the Word type library that ClassWizard presents. Add a button named IDC_RUN to the IDD_AUTOWORD_DIALOG dialog resource. Right-click the new button and click Events. Click Add Handler to add a handler for the BN_CLICKED event to the message map. Click OK to accept the OnRun member function name.  In AutoWord.cpp, append MSWord8.h (or MSWord9.h) to the list of includes: </li>  In AutoWordDlg.cpp, replace the OnRun handler void CAutoWordDlg::OnRun {    // TODO: Add your control notification handler code here } with the following code: _Application oApp; _Document oDoc; Documents oDocs; MailMergeFields oMMFields; COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR), vTrue((short)TRUE), vFalse((short)FALSE);
 * 1) include &quot;MSWord8.h&quot; //Use MSWord9.h for Word 2000.

void AddFieldAtSelection(CString sMergeField) {   Selection oSel = oApp.GetSelection; Range oSelRange = oSel.GetRange; MailMergeField oMMField = oMMFields.Add(oSelRange, sMergeField); }

void CAutoWordDlg::OnRun {   //Start Word. if(!oApp.CreateDispatch(&quot;Word.Application&quot;,NULL)) {       AfxMessageBox(&quot;Unable to start Word.&quot;); return; }

//Create a new mail merge document. oDocs = oApp.GetDocuments; oDoc = oDocs.Add(vOpt, vOpt); //for Word 97 //oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //for Word 2000 MailMerge oMerge = oDoc.GetMailMerge;

//Insert Fields into the document. oMMFields = oMerge.GetFields; Selection oSel; AddFieldAtSelection(&quot;Contact_Name&quot;); oSel = oApp.GetSelection; oSel.TypeParagraph; AddFieldAtSelection(&quot;Address&quot;); oSel = oApp.GetSelection; oSel.TypeParagraph; AddFieldAtSelection(&quot;City&quot;); oSel = oApp.GetSelection; oSel.TypeText(&quot; &quot;); AddFieldAtSelection(&quot;Postal_Code&quot;); oSel = oApp.GetSelection; oSel.TypeText(&quot; -- &quot;); AddFieldAtSelection(&quot;Country&quot;); oSel.ReleaseDispatch;

//Add an AutoText entry for the label layout. Template oNormal = oApp.GetNormalTemplate; AutoTextEntries oATEs = oNormal.GetAutoTextEntries; Range oRange = oDoc.GetContent; AutoTextEntry oATE = oATEs.Add(&quot;MyLabelLayout&quot;, oRange); oATEs.ReleaseDispatch; oNormal.ReleaseDispatch;

//Clear the document because the AutoText entry has been added and the document //text and fields are no longer needed. oRange.Delete(vOpt, vOpt); oRange.ReleaseDispatch;

//Set up the mail merge type as mailing labels and specify the data source. oMerge.SetMainDocumentType(1L); //1=wdMailingLabels oMerge.OpenDataSource(&quot;C:\\Data.txt&quot;, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);

//Create the new document for the merge using the AutoText entry previously added. MailingLabel oMLabel = oApp.GetMailingLabel; _Document oFinal = oMLabel.CreateNewDocument(COleVariant(&quot;5160&quot;), COleVariant(&quot;&quot;), COleVariant(&quot;MyLabelLayout&quot;), vOpt, COleVariant(4L)); oFinal.ReleaseDispatch; oMLabel.ReleaseDispatch;

//Execute the mail merge. oMerge.SetDestination(0);  //wdSendToNewDocument=0 oMerge.Execute(vOpt); oMMFields.ReleaseDispatch; oMerge.ReleaseDispatch;

//Delete the AutoText entry. oATE.Delete; oATE.ReleaseDispatch;

//Close the original mail merge document and make Word visible. oDoc.Close(vFalse, vOpt, vOpt); oDoc.ReleaseDispatch; oApp.SetVisible(TRUE); oDocs.ReleaseDispatch; oApp.ReleaseDispatch; } NOTE: If you did not save the tab-delimited text file as C:\Data.txt, modify the OpenDataSource method to specify the correct path to the file. Also note that if you wrapped the classes in the Word 2000 type library instead of the Word 97 type library, you must add two additional optional arguments to the call to the Add method, as illustrated in the code comment.

</li>  In AutoWord.cpp, add the following line to the beginning of the CAutoWordApp::InitInstance method: AfxOleInit; </li> Build the project.</li> Press F5 to run the project.</li> Click the IDC_RUN button.</li></ol>

A new document is presented in Word. The document consists of formatted mailing labels created with data that is extracted from the tab-delimited text file (C:\Data.txt).

<div class="references_section">