HOWTO: Invoke Word 6.0 Font Dialog for Word Object from VB

Last reviewed: September 29, 1997
Article ID: Q115985
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, version 3.0
- Microsoft Word 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:

   ARTICLE-ID: Q116040
   TITLE     : HOWTO: Run a Word Macro While Editing a Word Object in VB

It contains three examples that show you:

  1. How to place a button on a toolbar in a Visual Basic application to run a macro.

  2. How to call an assigned shortcut key in a Word macro from Visual Basic.

  3. How to call the nested main macro that calls the other macros from Visual Basic.

MORE INFORMATION

Step-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:

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

  2. Add a command button (Command1) and one MSOLE2.VBX control to Form1.

  3. 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.

  4. 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
    
    

  5. 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:

  1. 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
    
    

  2. 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 : 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.