Microsoft KB Archive/172483

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 11:06, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 172483

Article Last Modified on 1/20/2007



APPLIES TO

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Word 97 Standard Edition



This article was previously published under Q172483

SUMMARY

When using Microsoft Word 97 as an ActiveX server, you can run a Word VBA macro by using Word's Run method. However, the Run method with Word 97 cannot be used with Automation to run a macro that requires arguments. To run a Word macro that requires arguments, you can make your Word macro a method of your document or template. This article illustrates how to do this.

NOTE: The Run method has been updated in Word 2000 so that you can use that method to run a macro that requires arguments.

MORE INFORMATION

To make a macro act as a method for your document or template, add the macro as a Public Sub to the ThisDocument object in the Visual Basic Editor for Word.

The following steps demonstrate how to create a Visual Basic application that executes a custom method of a Word document. This method expects two arguments, a string and an integer.

Steps to Create the Word Macro

  1. Start a new Word document (Document1).
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. Click Project Explorer on the View menu to display the Project Explorer.
  4. In the Project Explorer, locate the "ThisDocument" object for Document1's project. Right-click "ThisDocument" and select View Code.
  5. In the code window for ThisDocument, add the following macro:

          Public Sub Macro1(s As String, i As Integer)
             MsgBox s        'Display the string
             MsgBox i * 10   'Display the product i and 10
    
          End Sub
                        
  6. Press ALT+Q to exit the Visual Basic Editor and return to Document1.
  7. Save this document as C:\TEST.DOC, and exit Word.

Steps to Create the Visual Basic Application

  1. Start a new "Standard EXE" project.
  2. On the Project menu, click References. Check "Microsoft Word 8.0 Object Library," and then click OK.
  3. Add the following code to Form1.

          Option Explicit
    
          Dim objWordApp As Word.Application
          Dim objWordDoc As Word.Document
    
          Private Sub Form_Click()
    
             'Create a new instance of Word
             Set objWordApp = New Word.Application
    
             'Open the document containing the macro
             Set objWordDoc = _
                objWordApp.Documents.Open("C:\TEST.DOC")
    
             'Run the macro
             objWordDoc.macro1 "Hello!", 23
    
             'Close the document and clear the variables
             objWordDoc.Close
             Set objWordDoc = Nothing
             Set objWordApp = Nothing
    
          End Sub
                        
  4. Press the F5 key to run the application. Click Form1 to run the Word macro.


Keywords: kbautomation kbhowto kbinterop KB172483