ID: Q120979
The information in this article applies to:
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:
WordBasic Syntax
----------------
FileNew .Template = "C:\WINWORD\TEMPLATE\LETTER1.DOT", .NewTemplate = 0
Visual Basic, Applications Edition, Syntax
------------------------------------------
.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.
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
The following sample Visual Basic for Applications module uses OLE Automation to send named arguments to Word and create a new document:
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.
The following is the WordBasic macro equivalent of the above Visual Basic for Applications module:
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
For the following mail merge example to work, you need to have Word 6.x running with a main mail merge document open.
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
KBCategory: kbmacro
KBSubcategory: kbmacro kbmerge
Additional reference words: winword 6.0 6.0a 6.0c 7.0 word95
word7 word6
Keywords : kbinterop kbmacro kbole kbmerge
Version : 6.0 6.0a 6.0c 7.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: November 18, 1998