来自MSDN
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q183369
How To Use Automation to Run a Word Macro with Arguments
View products that this article applies to.
Article ID | : | 183369 |
Last Review | : | January 22, 2007 |
Revision | : | 4.1 |
This article was previously published under Q183369
On This Page
SUMMARY
This article discusses how to use version 4.2 of the Microsoft Foundation Class (MFC) library installed with Microsoft Visual C++ to automate Word and run a macro that requires argument(s).
MORE INFORMATION
You can copy the code in this article to the message handler function of an event defined in an MFC .cpp file. However, the purpose of the code is to illustrate the process of using the IDispatch interfaces and member functions defined in the Msword type library. The primary benefit comes from reading and understanding the code so that you can modify the example, or write code from scratch to automate sending an argument to a Microsoft Word VBA macro.
This example presents two methods for passing arguments to Word VBA macros:
• | The first method calls a macro that is defined in the class module for the "ThisDocument" object. The macro definition specifies a parameter. |
• | The second method uses Word variable objects. The macro definition does not specify any parameters; however, the macro code accesses the value of a variable stored within the document. The macro is called using the Run method. |
NOTE: This method will not work for a document that is protected.
Step-by-Step Example
1. | In Microsoft Word, create a new document and press ALT+F11 to display the Visual Basic Editor. |
2. | In the Word Visual Basic Macro Editor "Project" box (usually located at the upper-left of the Visual Basic Window), double-click the tree node for "ThisDocument" to display its module window. Add the following code: |
3. | On the Insert menu, click Module to insert a new module and add the code below to the module: |
4. | Save the document as C:\Test.doc. Close the document and exit Word. |
5. | Follow steps 1 through 12 in the following Microsoft Knowledge Base article to create a sample project that uses the IDispatch interfaces and member functions defined in the MSWord type library: 178749 (http://support.microsoft.com/kb/178749/EN-US/) How To Create an Automation Project Using MFC and a Type Library |
6. | To the dialog box created in steps 4 and 5 of the parent article 178749 (http://support.microsoft.com/kb/178749/EN-US/), add an "Edit Box" control from the Controls toolbar (if that is not visible, right-click the gray area of the Visual Studio command bar. From the context pop-up menu, select "Controls.") This control will display the ID of IDC_EDIT1 in the General tab page of the Edit Properties dialog box. |
7. | On the View menu, click ClassWizard (or press CTRL+W). On the Member Variables tab of the MFC ClassWizard dialog, select IDC_EDIT1 and click Add Variable. Type "m_Argument" (without the quotation marks) for the Member Variable Name, and click Ok. Click OK to close the ClassWizard Dialog box. |
8. | At the top of the AutoProjectDlg.cpp file, add the following line: |
9. | Add the following code to CAutoProjectDlg::OnRun() in the AutoProjectDlg.cpp file: |
10. | You may need to modify the code in CAutoProjectDlg::OnRun() to indicate the correct path for your document Test.doc. The document is referenced in the following line: |
11. | Compile your VC++ project, then run it. |
12. | The dialog box will appear. Enter a string, such as "George Washington" (without the quotation marks) in the edit box. |
13. | Click Run in the dialog box. Word will appear, then the Message Box of the macro "GetSetVarVals()" will appear. The argument you entered in the MFC edit box has been passed to the Word VBA macro. |
REFERENCES
For more information about automating Microsoft Word using MFC, please refer to the following articles in the Microsoft Knowledge Base:
178784 (http://support.microsoft.com/kb/178784/EN-US/) How To Use Automation to Open and Print a Word Document
180312 (http://support.microsoft.com/kb/180312/EN-US/) How To Use Automation to Set the Printer from an MFC Project
For additional information about the Automation of Office applications, click the article number below to view the article in the Microsoft Knowledge Base:
222101 (http://support.microsoft.com/kb/222101/EN-US/) How To Find and Use Office Object Model Documentation