Article ID: 108043 - View products that this article applies to.
This article was previously published under Q108043
This article demonstrates how to use Microsoft Word version 6.0 Object Linking and Embedding (OLE) Automation from Visual Basic. Microsoft Word version 6.0 offers a single OLE object that supports most WordBasic statements and functions as methods. This allows you to create and run WordBasic code from Visual Basic. NOTE: The technique described in this article may not work if Microsoft Word version 6.0 is set to do background printing. When background printing is on, setting Word=Nothing may cause the Print Job to be canceled. If you encounter this problem, you can work around it by making the Word object variable's scope local to the form rather than to the Sub procedure. Or you can avoid the problem by turning background printing off (On is the default for Word for Windows). To turn background printing off, choose Options from the Tools menu. Then click the Print tab, and clear the checkbox for background printing.
Example of OLE AutomationYou can invoke the CreateObject function in Visual Basic using Word.Basic as the class name for the WordBasic object. The following example creates and uses a WordBasic OLE object:
The CreateObject function will launch Word version 6.0 if it is not already running, otherwise it will use the currently-active instance of Word.
The Set Word = Nothing statement will exit Word if Word was launched by the CreateObject statement.
OLE Automation cannot invoke the FileExit method of WordBasic. Because OLE Automation cannot start a new instance of Word after the initial instance, OLE Automation assumes that the user started Word and the user is responsible for exiting the application.
Troubleshooting Common Problems When Using OLE AutomationThe following are answers to common problems that you may encounter when using the Word.Basic OLE object from Visual Basic:
What is OLE Automation?Object linking and embedding (OLE) Automation is a Windows protocol that allows an application to share data or control another application. OLE Automation is an industry standard that applications use to expose their OLE objects to development tools, macro languages, and other containers that support OLE Automation.
Word for Windows provides other applications with an object called Basic and a class name called Word.Basic. Using this object, other applications can send WordBasic instructions to Microsoft Word version 6.0 for Windows.
Applications such as Visual Basic version 3.0 applications that support OLE Automation can use OLE Automation with Word version 6.0, but Word cannot use OLE Automation to gain access to other applications. Using the terminology of Dynamic Data Exchange (DDE), this means that Word can act as a server for another application but cannot act as the client.
A spreadsheet application may expose a worksheet, chart, cell, or range of cells, all as different types of OLE objects. A word processor might expose OLE objects such as application, paragraph, sentence, bookmark, or selection. You use Visual Basic to manipulate these objects by invoking methods on the object, or by getting and setting the objects properties, just as you would with the objects in Visual Basic.
"Visual Basic version 3.0: Programmer's Guide," Chapter 23, "Programming Other Applications' Objects."
See the following online Help topics in Visual Basic version 3.0: OLE Automation, CreateObject Function, Object Property, Set Statement
Article ID: 108043 - Last Review: January 8, 2003 - Revision: 1.1
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.