Accessing FrontPage Commands Using VBA
Microsoft Office FrontPage 2003
Microsoft FrontPage 2002
Microsoft FrontPage 2000
Summary: Learn how to access built-in Microsoft FrontPage commands that are not represented in the FrontPage Visual Basic for Applications (VBA) object model. (7 printed pages)
Introduction to Accessing FrontPage Commands
Understanding the Command Bar Objects
Using the Command Bar Objects
Accessing Features Not in the FrontPage Object Model
Finding the Correct Command Bar Names
If you work with the Microsoft FrontPage object model, you know that it doesn't provide programmatic access to all features in FrontPage. You might think that you have no way to programmatically access some features. But command bars can provide the access you need.
Command bars are part of the core Office VBA object model. The VBA object model for each Office application provides a CommandBars property to access the CommandBars collection for that application. Generally, when developers create add-ins for Office applications, they use the command bar objects to create custom menus, menu items, and toolbars. However, you can also use command bar objects to access built-in commands and dialog boxes. For FrontPage, this technique provides functionality that isn't provided through the object model.
When you use the CommandBars property, you get a CommandBars collection that contains a CommandBar object for each toolbar and menu in FrontPage. Each CommandBar object, in turn, contains a CommandBarControls collection (accessible through the Controls property) that contains a CommandBarControl object for each command in a menu or toolbar.
Hierarchically, the command bar objects resemble the diagram in Figure 1.
Figure 1. Command bar object hierarchy
Because the command bar objects access actual menus and toolbars in FrontPage, if a command that you want to access is not included in a menu or on a toolbar, you need to add it by using the Customize feature. (On the Tools menu, click Customize). For example, the Reformat HTML command is not included on any of the menus or toolbars in FrontPage when you first install the application. However, after you add the command to a menu or toolbar, you have full access to it in code by using the command bar objects.
To access a specific menu or toolbar (CommandBar object) you use its internal name (Name property), which is not necessarily visible in the FrontPage client. To access a specific command on a menu or toolbar (CommandBarControl object) you use the display text for the command (Caption property), which includes the ellipsis that follows some commands.
The simplest command bars to access are those on the main menus because you can easily identify the command bar names and command captions. The name of the command bar for a main menu is the same as the name of the menu. For example, the name of the CommandBar object for the File menu is "File", and the caption for the CommandBarControl object for the Save command on the File menu is "Save". The following code sample accesses the Save command on the File menu to save the active page:
Sub FileSave() CommandBars("File").Controls("Save").Execute End Sub
Many features in FrontPage are not represented in the FrontPage object model. In many cases, you can programmatically access commands for these features by using the command bar objects.
One example is copying, cutting, and pasting selected text and code. No methods in the object model can copy, cut, or paste. For this functionality, you could use the execCommand method of the IHTMLTxtRange object to access the Copy, Cut, and Paste commands. However, to use the execCommand method on a selection, you first need to create an IHTMLSelectionObject object to narrow the scope to include only the text or code that is currently selected. Then you need to create an IHTMLTxtRange object to access the execCommand method. That is a lot of code for a simple cut, copy, or paste operation.
Using the command bar objects, you can perform the same task with a single line of code. The following code samples show the code you need so that you can copy and cut selected text and paste the contents of the Clipboard into the current document.
Code Sample 1. Copy
Code Sample 2. Cut
Code Sample 3. Paste
Because the built-in copy and cut commands assume that the user has selected text, you do not need to define the selection or range as you would if you were using the IHTMLSelection and IHTMLTxtRange objects. However, if the user does not have any text selected, accessing these commands raises a run-time error.
Probably the most difficult part of using the command bar objects to access built-in FrontPage commands is determining which command bars and commands allow programmatic access, and then determining the name of the command bar and the caption for a command. It is beyond the scope of this article to provide a detailed list of all the command bar names and command captions available in FrontPage. However, the tips in this section should help you locate most, if not all, command bar names and command captions that you want to use.
Main menu commands are usually easy to determine. The name for a command bar that is part of the main menu bar is usually its display text, less the ampersand (&). So the command bar name for the File menu is File, the command bar name of the Edit menu is Edit, and so on. The caption text for a menu item is also usually easy to discover because it is the text that you see in the menu. If a menu command contains an ellipsis, programmatically accessing the command usually displays a dialog box or task pane. Accessing these commands is generally straightforward. However, if a menu item accesses a submenu, such as the Tasks command on the Edit menu, you need to do some investigating to locate the name of the command bar and the caption for the command that you want to access.
Enumerating the names of all command bars in FrontPage is easy. The following code sample displays the names in the Immediate window:
Sub GetCommandBarNames() Dim cmd As CommandBar For Each cmd In FrontPage.CommandBars Debug.Print cmd.Name Next End Sub
If you run this code, you may notice a single entry for the menu bar. Because the menu bar as a whole is a single CommandBar object, you need to drill into the controls for the Menu Bar CommandBar object to locate the individual commands. Then, to get the command captions for a specific command bar, you can loop through the CommandBarControls collection (Controls property) of a specific CommandBar object.
The following subroutine lists all the commands on the Standard toolbar. (The error handling in this code handles cases in which a command bar may not have controls.)
Sub GetCommandNames() Dim ctl As CommandBarControl Dim cmd As CommandBar On Error GoTo NoControls Set cmd = CommandBars("File") For Each ctl In cmd.Controls Debug.Print ctl.Caption Next Exit Sub NoControls: Debug.Print "No controls." 'Exit sub End Sub
All you need to do to use this subroutine is to change the text in quotes (shown as "File" in the preceding code sample) to the name of the CommandBar object for which you want to view the commands.
You can also use the FindControl method and the FindControls method to programmatically locate a control or collection of controls that fit specific search criteria. For more information on how to use these methods and code samples, see the appropriate topic.
Sometimes accessing a FrontPage command programmatically raises a run-time error. For these situations, you need to write appropriate error-handling code. This section describes a few of the situations when programmatically accessing a command bar command may raise a run-time error and how you can resolve those errors.
As mentioned earlier, the caption text for a command must include an ellipsis if an ellipsis is part of the command caption. The ampersand (&) that indicates the accelerator key is optional, but the ellipsis is not. For example, the Save As command on the File menu has a caption of "Save &As...". Both "Save As..." and "Save &As..." are valid strings for the Save As command. However, "Save As" and "Save &As" (without the ellipsis) are not valid captions and, if used, return a run-time error.
In addition, menu names must be accessed exactly as they appear, including leading and trailing spaces. For example, the Preview in Browser command contains an enumerated list of browsers in various resolutions. The order in which these browsers appear in the list depends on the order in which the user last accessed them. Programmatically accessing items in the enumerated list may raise a run-time error even if the string you use to access the item appears to be exact. For example, if Microsoft Internet Explorer is the first item in the list, the following code may appear to be correct:
CommandBars("Preview in Browser").Controls("1 Microsoft " & _ "Internet Explorer 6.0").Execute
However, this particular menu item has a trailing space; because the preceding code omits the space, running this code raises a run-time error. The correct command text is the following, which includes the trailing space:
CommandBars("Preview in Browser").Controls("1 Microsoft " & _ "Internet Explorer 6.0 ").Execute
Depending on the command, a CommandBarControl object may also be a CommandBar object that has CommandBarControl objects associated with it. For example, the Ruler and Grid command is both a CommandBarControl object and a CommandBar object. It is a CommandBarControl object of the View CommandBar object, but it is also a CommandBar object because it has a collection of CommandBarControl objects associated with it. Executing code against a CommandBar object raises a run-time error.
To illustrate, the following code accesses the Ruler and Grid command of the View menu:
CommandBars("View").Controls("Ruler and Grid").Execute
However, this code raises a run-time error because the Ruler and Grid command is a CommandBar object. Therefore, you need to access the Ruler and Grid command as a CommandBar object, and access the control under the Ruler and Grid command. For example, the following code accesses the Show Ruler command of the Ruler and Grid command:
CommandBars("Ruler and Grid").Controls("Show Ruler").Execute
A submenu on a larger menu might appear to be a CommandBar object, but you may find that executing code against that object raises a run-time error. For example, the Options command on the Code View toolbar contains a submenu of options that indicate how FrontPage handles code, but the Options submenu is not a CommandBar object. Executing code against any of the items in the submenu raises a run-time error. In this case, the Options submenu is called a popup menu, and you can access it through the CommandBarPopup object rather than through the CommandBarControl object. The following code toggles the Word Wrap command on the Options command of the Code View toolbar:
Dim objPopup As CommandBarPopup Set objPopup = CommandBars("Code View").Controls("Options") objPopup.Controls("Word Wrap").Execute
Programmatically accessing a command also raises an error when you attempt to execute a command that is unavailable in the current view. For example, the preceding code runs successfully only while a page is displayed in Code view (or when the code window is the active window in Split view). Executing this code while a page is displayed in Design view raises a run-time error. To mitigate errors, you can determine the current view before running the code or even change the view if necessary. The following code checks the current view, changes to Code view (if necessary), executes the Word Wrap command, and then switches back to the original view:
Sub ToggleWordWrap() Dim objPopup As CommandBarPopup Dim conView As FpPageViewMode 'Store current view mode. conView = ActivePageWindow.ViewMode 'Change view mode if view is not Code view. If conView <> fpPageViewHtml Then ActivePageWindow.ViewMode = fpPageViewHtml End If 'Toggle Word Wrap command. Set objPopup = CommandBars("Code View").Controls("Options") objPopup.Controls("Word Wrap").Execute 'Switch back to original view mode. ActivePageWindow.ViewMode = conView End Sub
Whether you are new to writing macros or are expert at developing add-ins, using the command bar objects to access FrontPage commands that are not represented in the object model expands the realm of possibilities for custom FrontPage solutions. Instead of deploying your add-in without access to a key FrontPage feature, you can now provide easy access to most, if not all, of the commands that you need.
The following articles may assist you as you create macros and add-ins for FrontPage: