HOWTO: Run a Word Macro While Editing a Word Object in VBLast reviewed: September 29, 1997Article 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
SUMMARYThis 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 INFORMATIONBelow 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 ToolbarYou 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:
Example Two: Calling an Assigned Shortcut Key in Your Word MacroYou 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:
Example Three: Calling the Main Macro That Calls the Other MacrosThis 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:
REFERENCESFor 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |