How to Use Named WordBasic Arguments in OLE Automation

Last reviewed: February 6, 1998
Article ID: Q120979
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0
  • Microsoft Excel for Windows, versions 5.0, 5.0a
  • Microsoft Visual Basic, Applications Edition

SUMMARY

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).

MORE INFORMATION

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.

Sample Visual Basic for Applications Module and WordBasic Macro

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

Sample Visual Basic for Applications Module to Run Mail Merge in Word

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

REFERENCES

"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 : kbmerge kbinterop kbmacro kbole kbmacro
Version : 6.0 6.0a 6.0c 7.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 6, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.