1. Start a new dialog-based MFC AppWizard EXE project named "MailMerge." 2. Using ClassWizard, add all of the wrapper classes for the Word type library. 3. Add a button to the dialog resource IDD_MAILMERGE_DIALOG and add the following code to the button's handler in MailMergeDlg.cpp: //For optional arguments COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR); //Start Word _Application oApp; oApp.CreateDispatch("Word.Application"); if(!oApp) { AfxMessageBox("Cannot start Word."); return; } //Get the Documents collection so that you can add //a new Document for the mail merge "main document" Documents oDocs = oApp.GetDocuments(); _Document oDoc = oDocs.Add(vOpt, vOpt); //Note for Word 2000: The Add method has 4 arguments in Word 2000. If //you wrapped the classes from the Word type library (msword9.olb), //modify the Add method to provide 4 optional arguments: // oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //Get the MailMerge property of the new document MailMerge oMMerge = oDoc.GetMailMerge();
//Set the document type as Form Letters oMMerge.SetMainDocumentType((long)0); //wdFormLetters = 0 //Set up the mail merge data source to the DSN "Northwind" and //an SQL statement CString sSQL; sSQL ="SELECT CompanyName, Address, ContactName, City, Country," \ "Region FROM Customers"; oMMerge.OpenDataSource("", vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, COleVariant("DSN=Northwind"), COleVariant(sSQL), vOpt); //Add the field codes and text to the document Selection oSel = oApp.GetSelection(); Range oRange; MailMergeFields oMMFlds = oMMerge.GetFields(); MailMergeField oMMFld; oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "CompanyName"); oSel.TypeParagraph(); oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "Address"); oSel.TypeParagraph(); oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "City"); oSel.TypeText(", "); oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "Country"); oSel.TypeParagraph(); oSel.TypeParagraph(); oSel.TypeText("Dear "); oRange = oSel.GetRange(); oMMFld = oMMFlds.Add(oRange, "ContactName"); oSel.TypeText(","); oSel.TypeParagraph(); oSel.TypeParagraph(); oSel.TypeText("This letter is to inform you..."); oSel.TypeParagraph(); oSel.TypeParagraph(); oSel.TypeText("Sincerely, John Smith"); //Execute the mail merge and then close the //main document without saving changes oMMerge.SetDestination(0); //wdSendToNewDocument = 0 oMMerge.Execute(COleVariant((short)FALSE)); oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt); //Make Word visible oApp.SetVisible(TRUE);
4. Add the following include to MailMergeDlg.cpp: #include "MSWord8.h" //or "msword9.h" for Word 2000 5. Modify CMailMergeApp::InitInstance() in MailMerge.cpp to start COM services: if(!AfxOleInit()) { AfxMessageBox("Cannot initialize COM services."); return FALSE; }6. Build and run the application. Click the button you added to the dialog box. When the Automation code finishes running, you will see a new document in Microsoft Word that consists of one form letter for each record in the Customers table.
dyw(旺仔) 的方法是我希望的雖然安裝了office2000但我還是找不到 msword8.h or msword9.h 程序就不能運行了!
msword8.h or msword9.h 是要从msword8.olb or msword9.olb 中export出来的。1 go to class wizard 2 click add class and select from type library... 3 pick msword8.olb or 9.olb 4 select the interface you wanted!
2. Using ClassWizard, add all of the wrapper classes for the Word type library.
3. Add a button to the dialog resource IDD_MAILMERGE_DIALOG and add the following code to the button's handler in MailMergeDlg.cpp:
//For optional arguments
COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR);
//Start Word
_Application oApp;
oApp.CreateDispatch("Word.Application");
if(!oApp)
{
AfxMessageBox("Cannot start Word.");
return;
} //Get the Documents collection so that you can add
//a new Document for the mail merge "main document"
Documents oDocs = oApp.GetDocuments();
_Document oDoc = oDocs.Add(vOpt, vOpt);
//Note for Word 2000: The Add method has 4 arguments in Word 2000. If
//you wrapped the classes from the Word type library (msword9.olb),
//modify the Add method to provide 4 optional arguments:
// oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt); //Get the MailMerge property of the new document
MailMerge oMMerge = oDoc.GetMailMerge();
//Set the document type as Form Letters
oMMerge.SetMainDocumentType((long)0); //wdFormLetters = 0 //Set up the mail merge data source to the DSN "Northwind" and
//an SQL statement
CString sSQL;
sSQL ="SELECT CompanyName, Address, ContactName, City, Country," \
"Region FROM Customers";
oMMerge.OpenDataSource("", vOpt, vOpt, vOpt, vOpt,
vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
COleVariant("DSN=Northwind"),
COleVariant(sSQL), vOpt); //Add the field codes and text to the document
Selection oSel = oApp.GetSelection();
Range oRange; MailMergeFields oMMFlds = oMMerge.GetFields();
MailMergeField oMMFld; oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "CompanyName");
oSel.TypeParagraph();
oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "Address");
oSel.TypeParagraph();
oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "City");
oSel.TypeText(", ");
oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "Country");
oSel.TypeParagraph();
oSel.TypeParagraph();
oSel.TypeText("Dear ");
oRange = oSel.GetRange();
oMMFld = oMMFlds.Add(oRange, "ContactName");
oSel.TypeText(",");
oSel.TypeParagraph();
oSel.TypeParagraph();
oSel.TypeText("This letter is to inform you...");
oSel.TypeParagraph();
oSel.TypeParagraph();
oSel.TypeText("Sincerely, John Smith"); //Execute the mail merge and then close the
//main document without saving changes
oMMerge.SetDestination(0); //wdSendToNewDocument = 0
oMMerge.Execute(COleVariant((short)FALSE));
oDoc.Close(COleVariant((short)FALSE), vOpt, vOpt); //Make Word visible
oApp.SetVisible(TRUE);
4. Add the following include to MailMergeDlg.cpp:
#include "MSWord8.h" //or "msword9.h" for Word 2000
5. Modify CMailMergeApp::InitInstance() in MailMerge.cpp to start COM services:
if(!AfxOleInit())
{
AfxMessageBox("Cannot initialize COM services.");
return FALSE;
}6. Build and run the application. Click the button you added to the dialog box. When the Automation code finishes running, you will see a new document in Microsoft Word that consists of one form letter for each record in the Customers table.
程序就不能運行了!
2 click add class and select from type library...
3 pick msword8.olb or 9.olb
4 select the interface you wanted!
其中FileName是要打开的文档名,可以*.txt或*doc;FilePath是文件路径