HOWTO: Run a Word Macro While Editing a Word Object in VB
ID: Q116040
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions 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:
- Start Microsoft Word version 6.0.
- Choose Customize... from the Tools menu.
- Press ALT+T to select the Toolbars tab.
- In the Categories list, scroll down to Macros and select "Macros."
A list of macros will show to the right.
- 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.
- 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:
- Start Microsoft Word version 6.0 for Windows.
- Choose Customize... from the Tools menu.
- Press ALT+K to select the Keyboard tab.
- In the Categories list, scroll down to Macros and select "Macros." A
list of macros will show to the right.
- Select the name of the macro (macFormatFont).
- Press ALT+N to go to the Press New Shortcut Key box.
- Press CTRL+SHIFT+M to create the shortcut key.
- Choose Assign to assign the key.
PART TWO -- Steps to Call the Shortcut Key from Visual Basic:
- Start a new project in Visual Basic. Form1 is created by default.
- Add a Command button (Command1) and one OLE 2.0 control (OLE1) to Form1.
- 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.
- Add the following code to the Command1_Click event procedure:
Sub Command1_Click()
Ole1.action = 7
SendKeys "^+M"
End Sub
- 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:
- Start Microsoft Word version 6.0 for Windows.
- Choose Macro... from the Tools menu. Name the macro Main, and choose
the Create button.
- 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
- Choose Macro... from the Tools menu. Name the macro Macro1, and
choose the Create button.
- Add the following code to the Macro1 macro:
Sub MAIN
msgbox "Successful run of macro1"
End Sub
- Choose Macro... from the Tools menu. Name the macro Macro2, and
choose the Create button.
- Add the following code to the Macro2 macro:
Sub MAIN
msgbox "Successful run of macro2"
End Sub
- Close and save each of these global macros.
PART TWO: Visual Basic Code:
- Start a new project in Visual Basic. Form1 is created by default.
- Add a Command button (Command1) and an OLE 2.0 control (OLE1) to Form1.
- 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.
- 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
- 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:
Q106282 Word Items Available When Word 6.0 Is in Server Mode
Additional query words:
officeinterop w_VBApp W_Word WM_OLEOA OLE Automation
Keywords : kbVBp300 kbDSupport IAPOLE
Version : WINDOWS:3.0,6.0
Platform : WINDOWS
Issue type : kbhowto