How To Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, Word 2002, or Word 2003

本文介绍如何使用MFCAutomation客户端调用Microsoft Word Basic功能,通过自动化实现特定文档的打印设置及打印操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  
View products that this article applies to.
Article ID : 252719
Last Review : March 23, 2006
Revision : 5.0
This article was previously published under Q252719
On This Page
 
SUMMARY
 
MORE INFORMATION
 
Steps to Create Sample MFC Automation Client
 
Additional Notes
 
REFERENCES
SUMMARY
This article demonstrates how to automate Microsoft Word from an MFC Automation client, and call WordBasic functions.
 Back to the top
 
MORE INFORMATION
Microsoft Word versions prior to Word 97 implemented WordBasic as their programming language. Word 97 introduced Microsoft Visual Basic for Applications (VBA) as its new standard, but for the purpose of maintaining backward compatibility, WordBasic functions can still be called. When you develop solutions for Word, Microsoft recommends that you use the Word object model instead of WordBasic, if you can. For additional information about WordBasic equivalents in the Word Object model, visit the following Microsoft Developer Network (MSDN) Web site:
 
Visual Basic Equivalents for WordBasic Commands
 
 
http://msdn.microsoft.com/library/en-us/vbawd10/html/wohowwordbasicequivalents.asp (http://msdn.microsoft.com/library/en-us/vbawd10/html/wohowwordbasicequivalents.asp)
 
 
However, if you need functionality provided by a specific WordBasic function for which you cannot find a suitable Word 97/2000/2002/2003 equivalent, then you can still call the WordBasic function.
 
While automating Word 97, Word 2000, Word 2002, or Office Word 2003 you can execute WordBasic functions if you first acquire an interface to the WordBasic object by calling the WordBasic method of the Application class. The WordBasic method returns an object of the COleDispatchDriver class; you can then use the object that is returned to invoke WordBasic functions.
 Back to the top
 
Steps to Create Sample MFC Automation Client
The following steps demonstrate how you can call the WordBasic FilePrintSetup function through Automation using named arguments.
 
The Word 97, Word 2000, Word 2002, and Office Word 2003 object models provide a property for changing the active printer: the ActivePrinter property. When you call the ActivePrinter property, it changes the system default printer. If you want to change only the active printer in Word and not the system default printer, you can use the WordBasic FilePrintSetup function instead.1. Create a new dialog-based MFC EXE project named WordPrint.
2. Open ClassWizard (CTRL+W), click the Automation tab, click Add Class, and then select From a type library. Browse to the folder where you installed Office and select the object library for Microsoft Word.
 
NOTE: With Word 97, the library is Msword8.olb. With Word 2000, the library is Msword9.olb. With Word 2002 and Office Word 2003, the library is Msword.olb
3. Select all the classes that the ClassWizard displays, and click OK.
4. Select the dialog resource IDD_WORDPRINT_DIALOG and add a button.
5. Add a BN_CLICKED-handler for the button with the following code:     _Application oWord ;
   Documents oDocs;
   _Document oDoc;
   COleDispatchDriver oWordBasic;
   COleVariant vOpt(DISP_E_PARAMNOTFOUND, VT_ERROR); //For optional args
 
   //Start Word
   if(!(oWord.CreateDispatch("Word.Application", NULL)))
 
   {
      AfxMessageBox("Error starting Word.", MB_SETFOREGROUND);
      return;
   }
 
   //Open a document
   oDocs = oWord.GetDocuments();
   oDoc = oDocs.Open(COleVariant("C://Doc1.doc"), vOpt, vOpt,
              vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
 
   // Note: If you are using the Word 2000 library, you will
   //      need to specify 12 arguments for the Open method
   //
   // oDoc = oDocs.Open(COleVariant("C://Doc1.doc"), vOpt, vOpt, vOpt,
   //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
   //
   // Note: If you are using the Word 2002 library, you will
   //      need to specify 16 arguments for the Open method
   //
   // oDoc = oDocs.Open(COleVariant("C://Doc1.doc"), vOpt, vOpt, vOpt,
   //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //           vOpt, vOpt, 0, vOpt);
 
   // Note: If you are using the Office Word 2003 library, you will
   //      need to specify 16 arguments for the Open method
   //
   // oDoc = oDocs.Open(COleVariant("C://Doc1.doc"), vOpt, vOpt, vOpt,
   //           vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //           vOpt, vOpt, vOpt, vOpt);
 
 
 
   oWordBasic = oWord.GetWordBasic();
 
   //Call the WordBasic FilePrintSetup function.
 
   HRESULT hr;
  OLECHAR FAR* szMethod[3];
   DISPID dispid[3];
 
   //Retrieve the DISPIDs for the function as well as two of its named
   //arguments, Printer and DoNotSetAsSysDefault
   szMethod[0]=OLESTR("FilePrintSetup"); //method name
 
   szMethod[1]=OLESTR("Printer");        //argument name
   szMethod[2]=OLESTR("DoNotSetAsSysDefault"); //argument name
 
   hr = oWordBasic.m_lpDispatch->GetIDsOfNames(IID_NULL, szMethod, 3,
                               LOCALE_USER_DEFAULT, dispid);
 
   //Invoke the FilePrintSetup function using named arguments.
   VARIANT vArgs[2];
   DISPPARAMS dp;
   dp.cArgs = 2;
 
   dp.cNamedArgs = 2;
   dp.rgvarg = vArgs;
   dp.rgdispidNamedArgs=&(dispid[1]); 
 
   vArgs[1].vt = VT_I2;
   vArgs[1].iVal = 1;     //DoNotSetAsSysDefault = 1
   vArgs[0].vt = VT_BSTR;
   vArgs[0].bstrVal = ::SysAllocString(OLESTR("Generic / Text Only"));
   //NOTE: You should replace "Generic / Text Only" in the line
   //above with the name of a printer installed on your system.
 
   hr = oWordBasic.m_lpDispatch->Invoke(dispid[0], IID_NULL,
              LOCALE_USER_DEFAULT,DISPATCH_METHOD, &dp, NULL, NULL, NULL);
 
   ::SysFreeString(vArgs[0].bstrVal);
 
   //Print the document
   oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt,
                 vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
                 vOpt, vOpt, vOpt, vOpt, vOpt);
 
   // Note: If you are using the Word 2000 or the Word 2002 library, you will
   //       need to specify 19 arguments for the Printout method
   //
   // oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt, vOpt,
   //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
 
   // Note: If you are using the Office Word 2003 library, you will
   //       need to specify 18 arguments for the Printout method
   //
   // oDoc.PrintOut(COleVariant((short)false), vOpt, vOpt, vOpt,
   //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt,
   //             vOpt, vOpt, vOpt, vOpt, vOpt, vOpt, vOpt);
 
 
   //Close the document without saving changes
   oDoc.Close(COleVariant((short)false), vOpt, vOpt);
 
   //Clean-up
   oDoc.ReleaseDispatch();
   oDocs.ReleaseDispatch();
   oWordBasic.ReleaseDispatch();
 
   //Quit Word
   oWord.Quit(COleVariant((short)false), vOpt, vOpt);
                                               
NOTE: The code above uses the Word 97 object library. If you are using the Word 2000, Word 2002, or Office Word 2003 object library, you need to modify the calls to Documents::Open() and Document::Printout() to include additional arguments. There are in-line comments in the code that describe the changes you should make for Word 2000, Word 2002, or Office Word 2003.
6. Modify the name of the printer in the aforementioned button handler to match the name of a printer installed on your system. The printer name in the code should appear the same as it does in the Printers section of your Control Panel.
7. Include the wrapper classes for the Word object model in WordPrinterDlg.cpp. For Word 97, use:#include "msword8.h"
 
                                       
and for Word 2000, use: #include "msword9.h"
                                       
and for Word 2002 or Office Word 2003, use: #include "msword.h"
                                       
 
8. Add the following line to CWordPrintApp::InitInstance in WordPrinter.cpp to initialize OLE libraries:AfxOleInit();
                                       
 
9. Create a Word document "c:/doc1.doc" that this Automation client can use.
10. Build and run the application.
11. When you click the button you added to the dialog, Word starts and prints the document ("c:/doc1.doc") to the printer you specified.
 
Additional Notes
The WordBasic functions are not defined in either the Word 97, the Word 2000, the Word 2002, or the Office Word 2003 object libraries. Therefore, the wrapper classes that the ClassWizard generates from these libraries do not contain WordBasic functions. You can view the Word 95 type library (wb70en32.tlb) in the OLE / COM Object Viewer. Please see the following article in the Microsoft Knowledge Base for information on how to obtain the Word 95 type library:
143434 (http://support.microsoft.com/kb/143434/EN-US/) WD: How to Obtain the Word for Windows Type Library
Documentation for WordBasic functions is not included with Word 97, Word 2000, Word 2002, or Office Word 2003. WordBasic functions are documented in the "Microsoft Word Developer's Kit" (ISBN:1-55615-880-7) and in the Help included with Microsoft Word 95.
 Back to the top
 
REFERENCES
For more information about the ActivePrinter property in Word 97, Word 2000, and Word 2002, please see the following Knowledge Base article:
 
216026 (http://support.microsoft.com/kb/216026/EN-US/) INFO: ActivePrinter Property in Word Sets System Default Printer
 
(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by Lori Turner, Microsoft Corporation.
 
 
 Back to the top
 
 
--------------------------------------------------------------------------------
 
APPLIES TO
• Microsoft Office Word 2003
• Microsoft Word 2002 Standard Edition
• Microsoft Word 2000 Standard Edition
• Microsoft Word 97 Standard Edition
• Microsoft Visual C++ 6.0 Professional Edition
• Microsoft Foundation Class Library 4.2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值