HOWTO: Run a Word Macro While Editing a Word Object in VB

Last reviewed: September 29, 1997
Article ID: Q116040

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows version 3.0
- Microsoft Word for Windows, version 6.0

SUMMARY

This article gives three examples to show you how to run a Word macro while editing a Word object in Visual Basic. You cannot run a Word macro directly by using the ToolsMacro command because you cannot run a Word macro while a Word object is activated in another application. When you attempt to run the Word ToolsMacro command, you are doing the same thing as choosing Macro... from the Word Tools menu. When acting as an in-place OLE server, Word enables only those menu items that apply to the object that is being edited in the OLE container application; the Macro... menu item is not one of them.

For example, if you embed a Word object in a Microsoft Publisher version 2.0 publication, Word knows it wouldn't make sense to choose Save from the File menu in Word to save the object. It is the container application's (Publisher's) responsibility to save its own documents and all the objects in them. In fact, when you choose Save from the File menu in Publisher while editing a Word object, Publisher saves the object because OLE objects cannot own the File menu. Therefore, menu items such as ToolsMacro are disabled.

MORE INFORMATION

Below are three examples that show you how to run a Word macro while editing a Word object in Visual Basic even though you can't use the ToolsMacro command. The samples use Word code, Visual Basic code, or both and they assume you have a macro called macFormatFont.

Example One: Placing a Button on the Word Toolbar

You can place a button on a toolbar to represent your macro. Then the user can click the button to run your macro while the document they are editing is in view. To create a button for your macro, follow these steps:

  1. Start Microsoft Word version 6.0.

  2. Choose Customize... from the Tools menu.

  3. Press ALT+T to select the Toolbars tab.

  4. In the Categories list, scroll down to Macros and select "Macros." A list of macros will show to the right.

  5. Click the name of the macro (macFormatFont), and drag it to one of the toolbars outside of the dialog on the main Word window. Drop it onto the toolbar. A selection of button images will be displayed.

  6. Click the button image you want, and choose Assign. Then click the Close button.

Example Two: Calling an Assigned Shortcut Key in Your Word Macro

You can assign a shortcut key to the macro. This will allow both your program and the user to activate the macro with a shortcut key. For example, you can assign the shortcut key CTRL+SHIFT+M to macFormatFont (see Part One). Then activate it with Visual Basic code from a command button click event procedure (see Part Two).

PART ONE -- Steps to Assign the Shortcut Key from Word:

  1. Start Microsoft Word version 6.0 for Windows.

  2. Choose Customize... from the Tools menu.

  3. Press ALT+K to select the Keyboard tab.

  4. In the Categories list, scroll down to Macros and select "Macros." A list of macros will show to the right.

  5. Select the name of the macro (macFormatFont).

  6. Press ALT+N to go to the Press New Shortcut Key box.

  7. Press CTRL+SHIFT+M to create the shortcut key.

  8. Choose Assign to assign the key.

PART TWO -- Steps to Call the Shortcut Key from Visual Basic:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a Command button (Command1) and one OLE 2.0 control (OLE1) to Form1.

  3. Select the Create from File option in the Insert Object window. Then choose the Browse button, and locate a document. Next, select the Display As Icon option, and choose the OK button.

  4. Add the following code to the Command1_Click event procedure:

          Sub Command1_Click()
             Ole1.action = 7
             SendKeys "^+M"
          End Sub
    
    

  5. Run the application and click the Command1 button to run your macro on the document while viewing and editing the document in Visual Basic.

Example Three: Calling the Main Macro That Calls the Other Macros

This example creates a main macro that controls the others. It reads a file that tells it which macro to run. Then it runs that macro. From the Visual Basic side, Visual Basic creates the file that tells the main macro which macro to run. Then the macro is run when the user presses the shortcut key (CTRL+SHIFT+M) assigned to it.

This example has two parts. Part one gives the code for the Main macro and sub-macros in Word. Part two gives the Visual Basic code.

PART ONE -- Main Macro in Word to Be Run by Pressing CTRL+SHIFT+M:

  1. Start Microsoft Word version 6.0 for Windows.

  2. Choose Macro... from the Tools menu. Name the macro Main, and choose the Create button.

  3. Add the following code to the Main macro:

       Sub MAIN
          Open "C:\MACRO.DAT" for input as #1
          line input #1,a$
          Close #1
          Kill "C:\MACRO.DAT"
          Select case a$
             Case "macro1": call macro1
             Case "macro2": call  macro2
          End select
       End Sub
    
    

  4. Choose Macro... from the Tools menu. Name the macro Macro1, and choose the Create button.

  5. Add the following code to the Macro1 macro:

       Sub MAIN
          msgbox "Successful run of macro1"
       End Sub
    
    

  6. Choose Macro... from the Tools menu. Name the macro Macro2, and choose the Create button.

  7. Add the following code to the Macro2 macro:

       Sub MAIN
          msgbox "Successful run of macro2"
       End Sub
    
    

  8. Close and save each of these global macros.

PART TWO: Visual Basic Code:

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a Command button (Command1) and an OLE 2.0 control (OLE1) to Form1.

  3. Select the Create from File option in the Insert Object window. Then choose the Browse button, and locate a document. Next, select the Display As Icon option, and choose the OK button.

  4. Add the following code to the Command1_Click event procedure:

       Sub Command1_Click()
          Open "C:\MACRO.DAT" for output as #1
          Print #1,"macro1"
          Close #1
          ole1.action = 7
          SendKeys ("^+M") ' This shortcut key must be assigned to Main macro
       End Sub
    
    

  5. Run the application. Click the Command1 button to have it run the Main macro, which in turn runs the macro with the shortcut key already assigned.

REFERENCES

For more information on this topic, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q106282
   TITLE     : Word Items Available When Word 6.0 Is in Server Mode


Additional query words: officeinterop w_VBApp W_Word WM_OLEOA OLE
Automation
Keywords : IAPOLE vbwin GnrlVb kbprg
Technology : kbole kbvba
Version : WINDOWS:3.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: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.