Microsoft KB Archive/120979

From BetaArchive Wiki

How to Use Named WordBasic Arguments in OLE Automation

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 for Applications





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

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.