How to Use Named WordBasic Arguments in OLE Automation |
Q120979
This article describes how to create a routine in Microsoft Visual Basic for Applications that uses named and position arguments to send commands to Word for Windows using OLE Automation. For example, you can send OLE Automation commands to Word from a Microsoft Excel Visual Basic for Applications macro to perform a print merge operation in Word (see the "Sample Visual Basic for Applications Module to Run Mail Merge in Word" section at the end of this article).
The syntax for named arguments in Visual Basic, Applications Edition, is similar to the WordBasic syntax in Word for Windows. You can see the similarities in the following sample WordBasic and Visual Basic for Applications syntax, both of which start a new Word document based on the LETTER1.DOT template:
FileNew .Template = "C:\WINWORD\TEMPLATE\LETTER1.DOT", .NewTemplate = 0
.FileNew Template := "c:\winword\template\letter1.dot", NewTemplate := 0
NOTE: The syntax differences are small but significant. In Visual Basic for
Applications, the Word command is preceded by a period (.), there is no
period (.) before the command arguments, and a colon (:) appears before the
equal sign (=). If your Visual Basic for Applications syntax is incorrect,
the following error message may occur:
Object doesn't support this property or method.
Sub Test()
Dim Word As Object
Set Word = CreateObject("word.basic")
With Word
.FileNewDefault
.Insert "This text appears here because of OLE Automation."
.StartOfLine 1
.FormatFont Points:="12", SmallCaps:=1, Font:="Courier New", Bold:=1
.StartOfDocument
.FormatParagraph Alignment:=1, WidowControl:=0
End With
End Sub
NOTE: Word should be active before you run this Visual Basic for
Applications module. If Word is not running, you will not see the result in
Word because when OLE Automation starts Word, OLE closes Word after the
Visual Basic for Applications module runs.
Sub MAIN
FileNewDefault
Insert "This text appears here because of OLE Automation."
StartOfLine 1
FormatFont .Points = "12", .SmallCaps = 1, .Font = "Courier New", .Bold = 1
StartOfDocument
FormatParagraph .Alignment = 1, .WidowControl = 0
End Sub
Sub Print_Merge()
Set wordobject = CreateObject("word.document.6")
Set wordbasic = wordobject.Application.wordbasic
With wordbasic
.MailMerge CheckErrors:=1, Destination:=0, MergeRecords:=0,
From:="", To:="", Suppression:=0, MailMerge:=1, MailSubject:="",
MailAsAttachment:=0, MailAddress:=""
End With
End Sub
"Microsoft Word Developer's Kit," version 6.0, Microsoft Press, 1994, pages 174-182
Additional query words: winword 6.0 7.0 word95 word7 word6
Keywords : kbinterop kbmacro kbole kbmerge
Issue type : kbhowto
Technology : kbWordSearch kbExcelSearch kbWord700Search kbPTNotAssigned kbExcel500 kbZNotKeyword2 kbVBASearch kbZNotKeyword3 kbWord600 kbWord600a kbWord600c kbWord700 kbExcel500a
|
Last Reviewed: January 16, 2001 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |