This article was previously published under Q108043
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.
This article demonstrates how to use Microsoft Word version 6.0 ObjectLinking and Embedding (OLE) Automation from Visual Basic. Microsoft Wordversion 6.0 offers a single OLE object that supports most WordBasicstatements and functions as methods. This allows you to create and runWordBasic code from Visual Basic.NOTE: The technique described in this article may not work if MicrosoftWord version 6.0 is set to do background printing. When background printingis on, setting Word=Nothing may cause the Print Job to be canceled. If youencounter this problem, you can work around it by making the Word objectvariable's scope local to the form rather than to the Sub procedure. Or youcan avoid the problem by turning background printing off (On is thedefault for Word for Windows). To turn background printing off, chooseOptions from the Tools menu. Then click the Print tab, and clear thecheckbox for background printing.
Example of OLE Automation
You can invoke the CreateObject function in Visual Basic usingWord.Basic as the class name for the WordBasic object. The followingexample 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 Sub
The CreateObject function will launch Word version 6.0 if it is notalready running, otherwise it will use the currently-active instance ofWord.
The Set Word = Nothing statement will exit Word if Word was launched bythe CreateObject statement.
OLE Automation cannot invoke the FileExit method of WordBasic. BecauseOLE Automation cannot start a new instance of Word after the initialinstance, OLE Automation assumes that the user started Word and the useris responsible for exiting the application.
Troubleshooting Common Problems When Using OLE Automation
The following are answers to common problems that you may encounter whenusing the Word.Basic OLE object from Visual Basic:
The CreateObject function could cause an error under any of the following circumstances:
Word is not registered in the Windows REG.DAT file.
Windows is low on system resources.
Your user-defined NORMAL.DOT template and/or automatically loading macros in Word could run automatic actions that might conflict with your requested OLE Automation commands.
The OLE server application is not found. With Windows version 3.1, object linking and embedding (OLE) clients look for a server application in the following order:
The location specified in the Windows REG.DAT file.
The location specified in the WIN.INI file.
The WINDOWS directory.
The WINDOWS\SYSTEM directory.
The location specified in the MS-DOS PATH environment variable (which is specified in the AUTOEXEC.BAT file).
The WordBasic language allows certain shortcuts that are not supported by Visual Basic. For example, the following statement is valid in WordBasic but not in Visual Basic:
FormatFont .Bold = 1
Visual Basic does not support named parameters, such as .Bold above. Visual Basic requires you to convert this to the following:
Dim Word As Object 'Declare an object variable Set Word = CreateObject("Word.Basic") 'Set the object pointer Word.FormatFont ,,,,,,,,,,,,,,,,True 'Format selection as bold.
Fortunately, many WordBasic methods are implemented with more than one method, which can simplify the syntax required by Visual Basic. For example, WordBasic has a direct Bold method which you can invoke as follows from Visual Basic:
Visual Basic requires you to pass all arguments up to the last necessary argument. The following example shows the arguments for the ToolsMacro method of WordBasic.
To run a Word macro, use this syntax:
Dim Word As Object 'Declare an object variable Set Word = CreateObject("Word.Basic") 'Set the object pointer Word.ToolsMacro "MyMacro", True 'Run the macro called MyMacro
To rename a Word macro, use this syntax:
'VB3Line: Enter the following lines as one line Word.ToolsMacro "MyMacro", False, False, 0, False, True, _ "Description for Your Macro", "NewMacroName"
The online Help for Microsoft Word version 6.0 doesn't always show the arguments in the correct order. For example, Word ToolsMacro parameters should be in this order:
WordBasic methods that return strings have a syntax that includes a dollar sign, $, to indicate the return type. Visual Basic requires you to enclose these $ methods in square brackets . The following example returns the text stored in the bookmark "MyBookMark":
Dim Word As Object 'Declare an object variable Set Word = CreateObject("Word.Basic") 'Set the object pointer MyVar = Word.[GetBookMark$]("MyBookMark") 'Return text from bookmark
What is OLE Automation?
Object linking and embedding (OLE) Automation is a Windows protocol thatallows an application to share data or control another application. OLEAutomation is an industry standard that applications use to expose theirOLE objects to development tools, macro languages, and other containersthat support OLE Automation.
Word for Windows provides other applications with an object calledBasic and a class name called Word.Basic. Using this object, otherapplications can send WordBasic instructions to Microsoft Wordversion 6.0 for Windows.
Applications such as Visual Basic version 3.0 applications that support OLEAutomation can use OLE Automation with Word version 6.0, but Word cannotuse OLE Automation to gain access to other applications. Using theterminology of Dynamic Data Exchange (DDE), this means that Word can act asa server for another application but cannot act as the client.
A spreadsheet application may expose a worksheet, chart, cell, or rangeof cells, all as different types of OLE objects. A word processormight expose OLE objects such as application, paragraph, sentence,bookmark, or selection. You use Visual Basic to manipulate these objectsby invoking methods on the object, or by getting and setting the objectsproperties, just as you would with the objects in Visual Basic.