Microsoft KB Archive/108043

= HOWTO: VB Uses OLE Automation with Word Version 6.0 =

Article ID: 108043

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Word 6.0 Standard Edition

-



This article was previously published under Q108043



SUMMARY
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 Automation
You 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 Sub 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 Automation
The following are answers to common problems that you may encounter when using 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.</li> The WINDOWS directory.</li> The WINDOWS\SYSTEM directory.</li> The location specified in the MS-DOS PATH environment variable (which is specified in the AUTOEXEC.BAT file).</li></ol> </li></ul> </li>  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:

Word.Bold

</li>  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" </li>  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: ToolsMacro .Name = text [, .Run][, .Edit][, .Show = number][, .Delete] [,.Rename] [, .Description = text][, .NewName = text][, .SetDesc] </li>  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 </li></ol>

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.

<div class="references_section">