How To Use WordBasic Functions in an MFC Automation Client for Word 97, Word 2000, Word 2002, or Word 2003 (252719)
The information in this article applies to:
- Microsoft Office Word 2003
- Microsoft Word 2002
- Microsoft Word 2000
- Microsoft Word 97 for Windows
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- The Microsoft Foundation Classes (MFC)
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 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. 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. - 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 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 - Select all the classes that the ClassWizard displays, and
click OK.
- Select the dialog resource IDD_WORDPRINT_DIALOG and add a
button.
- 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. - 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.
- 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"
- Add the following line to CWordPrintApp::InitInstance in WordPrinter.cpp to initialize OLE libraries:
AfxOleInit();
- Create a Word document "c:\doc1.doc" that this Automation
client can use.
- Build and run the application.
- 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 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. REFERENCES For more information about the ActivePrinter property in
Word 97, Word 2000, and Word 2002, please see the following Knowledge Base
article: 216026 INFO: ActivePrinter Property in Word Sets System Default Printer
(c) Microsoft Corporation 2000, All Rights
Reserved. Contributions by Lori Turner, Microsoft Corporation.
Modification Type: | Major | Last Reviewed: | 3/23/2006 |
---|
Keywords: | kbAutomation kbhowto KB252719 kbAudDeveloper |
---|
|