HOWTO: Invoke Word 6.0 Font Dialog for Word Object from VB
ID: Q115985
|
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 shows by example how to show the Word font dialog directly
in Visual Basic without using a Word macro.
You cannot run macros in Word for Windows while a Word object is activated
in a container application. When you attempt to run the ToolsMacro command,
you are doing essentially the same thing as choosing Macro... from Word's
Tools menu. When a Word object is active in a container, Word enables only
those menu items that apply to editing in a container application; the
Macro... item is not one of those. However, there are techniques you can
use to run a Word macro while editing a Word object in Visual Basic, so if
you want to know how to run Word macros, please see the following Microsoft
Knowledge Base article:
Q116040 HOWTO: Run a Word Macro While Editing a Word Object in VB
It contains three examples that show you:
- How to place a button on a toolbar in a Visual Basic application to run
a macro.
- How to call an assigned shortcut key in a Word macro from Visual Basic.
- How to call the nested main macro that calls the other macros from
Visual Basic.
MORE INFORMATIONStep-by-Step Example Calls Pre-Assigned Shortcut Key for Font Dialog
To show the Word font dialog directly without using a Word macro, call a
pre-assigned shortcut key. Because the shortcut key for showing the font
dialog is CTRL+SHIFT+F, you can use the Visual Basic SendKeys function to
activate it. Documentation for the shortcut keys is in the Word Help menu
under the "Keyboard Shortcuts" topic.
The following steps show the font dialog when the user clicks a Visual
Basic command button:
- Start a new project in Visual Basic. Form1 is created by default.
- Add a command button (Command1) and one MSOLE2.VBX control to Form1.
- Select the Create from File option in the Insert Object window.
Then click the Browse button and locate a document. Next, select the
Display As Icon option, and click the OK button.
- Add the following code to the Command1_Click event procedure:
Sub Command1_Click()
ole1.action = 7
SendKeys "^+F" '** note: you may need to send a second SendKeys "^+F"
End Sub
- Press the F5 key to run the application. Click the Command1 button to
see if the font dialog window and Formatting toolbar appear.
Alternative Code for Step 3 to Hide the Formatting Toolbar
If the Word Formatting Toolbar is showing, the code shown above will
actually select the Edit Font dialog box, so you'll need to hide the
Toolbar first if this is the case. The following code demonstrates how to
hide the Format Toolbar and show it again after the user clicks the command
button and chooses a font:
- Add the following code to the Command1_Click event procedure:
Sub Command1_Click()
Dim Wordobj as Object
ole1.action = 7
Set Wordobj = ole1.Object.application.wordbasic
Wordobj.ViewToolbars "Formatting",,,,,,,,True ' Eight commas
SendKeys "^+F"
DoEvents ' Pause to let the sendkeys go through to Word.
Wordobj.ViewToolbars "Formatting",,,,,,,True ' Seven commas
Set Wordobj = Nothing
End Sub
- Run the application, and click the Command1 button to see if the
font dialog window appears without the Formatting toolbar.
NOTE: The user can click the MSOLE2 control by using the right mouse button
to bring up a menu of Word options. One of the options is to change the
font. By choosing this option, the user invokes the Font dialog box. This
is yet another way for the user to get to the font dialog.
Additional query words:
officeinterop w_VBApp W_Word WM_OLE OA OLE Automation
Keywords : kbprg IAPOLE vbwin
Version : WINDOWS:3.0,6.0
Platform : WINDOWS
Issue type : kbhowto
|