MacroButton Fields - not just for macros anymore!

Syntax 

{ MacroButton MacroName DisplayText }

MacroButton - the field command

MacroName - name of macro being called - or "NoMacro"

DisplayText - text that acts as the "button" for the macro and is displayed in your document. Note that there are no quotation marks used in this field. DisplayText can be any text you want. There are probably limits on length. Instead of (or in addition to) the DisplayText you can insert a picture or icon to act as a visual button for your macro.

Behavior

Clicking on a MacroButton field will select the field. Double-clicking on one will activate a macro if there is a macro designated. You can change this behavior to make the macro activate on a single click as well.

MacroButton Fields as Prompts

You can use the first behavior (selecting the field) to make simple click-and-type prompts. If you look at many of the templates that come from Microsoft with Word, you will find places that say something like "[Click here and type]." If you click at that spot, the whole prompt is selected and anything you type replaces it. Often, this is exactly what you want for a simple prompt. What you are seeing is a simple implementation of the "MacroButton" field.

To insert a MacroButton field like this in your document:

Insert => Field ... 

Where it says Field: type "MacroButton NoMacro prompt." The "MacroButton" is the name of the field. "NoMacro" is the name of the macro - in this special case, none. The "prompt" is whatever prompt text you want. This can be "[Click here and type]" or the text that will usually be just fine but which the user might want to change.

As is the case with other fields, the F11 key will take you to the next field, selecting that field. When you use macrobutton fields as prompts, you may want to put an instruction in your documents to use F11 to go to the next field. I do this putting the instruction in blue or red text and a different font in a textbox formatted to float in front of text. The textbox is formatted to have no lines and no fill. The text in the textbox is formatted as hidden so it should not print. I have this textbox as an AutoText entry in my developer's template so that I can insert it easily when I prepare a new template.

MacroButton Fields to Run Macros

Instead of simply being a prompt for typing, this MacroButton field will run a macro if double-clicked. (This can be altered to a single click using VBA.) Instead of NoMacro, simply type the name of the macro. When inserting the field, you can click on the Options button and you will be given a list of available macros (possibly quite a long list) from which to select.

Where it says "Macros defined in active document" a more accurate caption would be "Macros available to active document." This list also includes all of Word's built-in commands, many of which are not found on the menus.

Note that the MacroButton field is selected when it is clicked or double-clicked. You may want to have your macro collapse the selection at some point so that your MacroButton field won't be inadvertently deleted. The language for this is:

Selection.Collapse 
'Unselects the current selection and places 
'insertion point at beginning of selection.

Note also that a macro button prompt will print as ordinary text. (It is the field result). To avoid it being printed, you may want to put it in a no-border text box formatted as hidden text (Format => Font). Because of this, it is often easier to use a custom toolbar with a button on that toolbar. The toolbar will not print.

MacroButton Fields can be used in Protected Forms and will be active even in a protected portion of the document. They are often used in such forms as a replacement for hyperlinks (since regular hyperlinks are inactive in the protected portion of a form).

A simple example of macrobuttons can be found in the CheckBox template. This template/tutorial combines the powers of macros, autotext, and the macrobutton field.

MacroButton Fields where the Macro responds to the contents of the field (uses them as an argument or variable for the macro)

It is possible to use one macro that responds to the contents of the field to change what the macro does. Doing this with Private or AddIn fields incorporated in MacroButton fields is discussed in the MacroButtons page on the MVP FAQ site. The same technique can be used with just the display text of the MacroButton field.

The macro is:

Sub TestMacro2()
Dim MyString As String
    'Ignore first 24 characters of the macrobutton field -
    '   the words 'MacroButton TestMacro2', and the spaces
    MyString = Mid$(Selection.Fields(1).Code, 24)
    MsgBox MyString
End Sub

The field is:

{ MacroButton TestMacro2 [Click Here] }

This can be used to construct a list of templates in the Workgroup Templates folder as macrobuttons. When you double-click (or single-click as shown below) on the macrobutton, the macro creates a new document based on the named template. A single macro decides which template to open based on the template listed in the macrobutton field.

That is, the following fields both call the same macro:

{ Macrobutton TeemplateListLoad Releases\Release - blank}

{ Macrobutton TeemplateListLoad Log}

The macro uses the display information to decide which template to use. The macro is:

Sub TemplateListLoad()
'   Based on ideas from http://www.mvps.org/word/FAQs/TblsFldsFms/UsingMacroButton.htm
'   Macro written by Charles Kyle Kenyon
'   24 October 2002
'
    Dim sTemplateName As String
    Dim sTemplatesPath As String
    '
    '   Get workgroup templates path
    '
    sTemplatesPath = Options.DefaultFilePath(wdWorkgroupTemplatesPath) & "\"
    '
    '   Parse template name from Macrobutton field.
    '   (The selection is the entire field.)
    On Error GoTo ErrorHandler
    sTemplateName = Mid$(Selection.Fields(1).Code, 31) & ".dot"
    '   Use to create a new document based on the template
    Documents.Add Template:=sTemplatesPath & sTemplateName
    Selection.Collapse
    Exit Sub
    '
End Sub

Making MacroButtons respond to a single click

To me, double-clicking on a button is counter-intuitive. To make a macrobutton respond to a single click the following VBA code has to be active:

Options.ButtonFieldClicks = 1

This has to be run before the user tries to click on the button. It can be in an AutoOpen or AutoNew macro in the template which contains the macrobutton or in an AutoExec macro in a global template. (Explaining these gets beyond the scope of this article; see Template Basics for more.)

Using MacroButtons to function as hyperlinks in protected forms.

In a document that has been "protected" as a form in Word, hyperlink fields don't work, but macrobutton fields do work. You can use a macrobutton for a hyperlink to an external document or web page (and even format it to look like a hyperlink) in a protected document. It is more work than merely inserting a hyperlink, though. (You can also use a macrobutton to link to an internal bookmark with reservations, see end of this section.)

  1. With your document not protected for forms, record a macro that opens the document to which you want to link. Save the macro in your document (not in Normal.dot) when you record it.
  2. Use the Macro Editor (Alt-F11) to edit your macro. At the end of the macro (just before "End Sub") add the following line.
      Selection.Collapse
  3. Where you want your hyperlink Press Ctrl-F9 to insert your field codes and type "MacroButton MyMacro displaytext." where "MyMacro" is the name of your macro and "displaytext" is what you want the hyperlink to show in your document. (See Syntax above)
  4. Press F9 to update your field. It should show your displaytext.
  5. Select your field and press Ctrl-Shift-S to get into the styles drop-down.
  6. Type "hyperlink" for the style name and press enter.

When you protect your document your pseudo-hyperlink should work fine. Remember to set the ButtonFieldClicks to 1. Also, the mouse pointer will not change to a little hand when passing over your pseudo-hyperlink - but we can't have everything, can we?

If you would like some pre-written code for your hyperlink macrobutton, you can download HyperJmp.zip from the Visual Basic MVP site.

For alternative coding, take a look at Using Hyperlinks in Protected Forms on the MVP site. They show how to use a single macro for all your hyperlinks in the document.

When the target of the link is within the protected form it doesn't work quite so well. It will jump to the field addressed by your bookmark if your bookmark is for a formfield, otherwise to the field following your bookmark. If there is no field following your non-field bookmark, it jumps to the first field in the document. Note that fields inserted with the Forms toolbar automatically have a bookmark assigned. (You can change that bookmark in the field's properties.) Thanks to Marcy T. for bringing this problem to my attention.

For more on protected forms, follow the links in my web resources page, especially those to Dian Chapman's excellent series of articles.

Additional references on MacroButton fields

Click to return to table of contents page of Legal Users' Guide to Microsoft Word.Click to go to Microsoft Word new users frequently asked questions site in a new browser window.
(this guide table of contents) ----- (MS Word New Users FAQ)