WD: How to Use OLE Automation with Word 6.0/7.0 |
Q161547
This article provides an overview of using OLE Automation with the versions
of Word for Windows listed at the beginning of this article.
NOTE: Throughout this article, examples utilizing OLE Automation assume
that the following lines of Visual Basic code have been used to initiate
the OLE Automation session:
Dim WBO As Object 'Declare an object variable
Set WBO = CreateObject("Word.Basic") 'Set the object pointer
Word versions 6.x and 7.x can be OLE Automation servers. Word makes available a single object, Word.Basic, allowing you to issue WordBasic commands from another application to control actions in Word. If you wish to use WordBasic to control other applications, you must use DDE.
Sub WordExample ()
Dim WBO As Object 'Declare an object variable
Set WBO = CreateObject("Word.Basic") 'Set the object pointer
WBO.FileNew 'Create a new File in Word
WBO.Bold 'Make the Font Bold
WBO.FontSize 24 'Make the Font 24 point in size
WBO.CenterPara 'Center Text on page
WBO.Insert "Automation's Great!" 'Insert some text
WBO.FilePrintDefault 'Print the current document
WBO.FileClose 2 'Close file without saving.
Set WBO = Nothing 'Clear the object pointer.
End Sub
The CreateObject function launches Word if it is not already running.
Otherwise it uses the currently-active instance of Word.
WBO.FormatFont ,,,,,,,,,,,,,,,,True 'Format selection as bold.
Named arguments allow you to specify only those arguments you wish to use.
They can be in any order and, because the arguments are named, the code is
generally more readable. For example:
WBO.FormatFont Bold:=True 'Format selection as bold.
It is generally better to use named arguments whenever possible. However,
Visual Basic 3.0 for Windows supports using only positional arguments.
For additional information, please see the following article(s) in the Microsoft Knowledge Base:
Q105534 OLE: Visual Basic 3.0 Does Not Support Named Arguments
Q112733 POSITION.HLP File for VB OLE Automation w/ Word for Windows
EditBook .Name="test", .Add
OLE Automation does not use the same type of syntax. Because OLE Automation
is programmed in another application, you must use that application's
syntax for passing OLE Automation arguments. Visual Basic, for example,
does not use periods before argument names, and a colon must be used before
the equal sign (:=). The following sample OLE Automation syntax using
Visual Basic or Visual Basic for Applications is typical:
WBO.EditBookmark Name:="test", Add:=True
MyVar = WBO.[GetBookMark$]("MyBookMark") 'Return text from bookmark
Without using the square brackets, Visual Basic version 3.0 returns the
error, Bad Parameter.
EditBookmark .Name="test", .Add
However, the following syntax is also valid:
EditBookmark .Name="test", .Add=True
When using OLE Automation, you must specify the Boolean value. Using named
arguments with OLE Automation, the command would be:
WBO.EditBookmark Name:="test", Add:=True
Using positional arguments, the command would be:
WBO.EditBookmark "test", , True
WBO.ToolsOptionsPrint Background:=0
For example, when background printing is on, setting WBO=Nothing may cause
the print job to be canceled. If you encounter this problem, you can also
work around it by making the Word object variable's scope local to the form
rather than to the Sub procedure.
Additional query words: 6.0 winword word7 word95
Keywords : kbinterop kbole word6 word7 word95 word kbofficeprog
Issue type : kbprb
Technology :
|
Last Reviewed: March 28, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |