Microsoft KB Archive/252719

= How to use WordBasic functions in an MFC Automation client for Word =

Article ID: 252719

Article Last Modified on 4/18/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * 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

-



This article was previously published under Q252719



SUMMARY
This article demonstrates how to automate Microsoft Word from an MFC Automation client, and call WordBasic functions.



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://msdn2.microsoft.com/en-us/library/aa158432(office.10).aspx

However, if you need functionality provided by a specific WordBasic function for which you cannot find a suitable Word equivalent, then you can still call the WordBasic function.

While automating Word 97 and later versions of Word, 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.

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 object model provides 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.  Create a new dialog-based MFC EXE project named WordPrint . 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 one of the following object libraries for Microsoft Word:  For Word 2002 and for later versions of Word, select Msword.olb. For Word 2000, select Msword9.olb. For Word 97, select Msword8.olb.  Select all the classes that the ClassWizard displays, and click OK.</li> Select the dialog resource IDD_WORDPRINT_DIALOG and add a button.</li>  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 or the Office Word 2007 // 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);



//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 object library for a later version of Word, you mustmodify 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, for Word 2002, for Office Word 2003, or for Office Word 2007. </li> 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.</li>  Include the wrapper classes for the Word object model in WordPrinterDlg.cpp. For Word 97, use: and for Word 2000, use: and for Word 2002, Office Word 2003, or Office Word 2007, use:  Add the following line to CWordPrintApp::InitInstance in WordPrinter.cpp to initialize OLE libraries: AfxOleInit; </li> Create a Word document that this Automation client can use.
 * 1) include "msword8.h"
 * 1) include "msword9.h"
 * 1) include "msword.h" </li>

For example, create one of the following Word documents:  In Word 2007, create the Word document "C:\doc1.docx".</li> In Word 2003 and in earlier versions of Word, create the Word document "C:\doc1.doc".</li></ul> </li> Build and run the application.</li> When you click the button you added to the dialog, Word starts and prints the document to the printer you specified.</li></ol>

Additional Notes
The WordBasic functions are not defined in the object libraries for Word 97 and the later versions of Word. 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 WD: How to Obtain the Word for Windows Type Library

Documentation for WordBasic functions is not included with Word 97 and later versions of Word. 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.

<div class="references_section">