HOWTO: VB Uses OLE Automation with Word Version 6.0Last reviewed: September 29, 1997Article ID: Q108043 |
The information in this article applies to:
SUMMARYThis 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.
MORE INFORMATION
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:
Sub WordExample () Dim Word As Object 'Declare an object variable Set Word = CreateObject("Word.Basic") 'Set the object pointer Word.FileNew 'Create a new File in Word Word.Bold 'Make the Font Bold Word.FontSize 24 'Make the Font 24 point in size Word.CenterPara 'Center Text on page Word.Insert "Isn't VB Great!!" 'Insert some text Word.FilePrintDefault 'Print the current document Word.FileClose 2 'Close file without saving. Set Word = Nothing 'Clear the object pointer. End SubThe 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.
REFERENCES"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
|
Additional query words: OLE2 OA winword kbmacro officeinterop
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |