Using MacroButton fields

Article contributed by Graham Mayor, Jonathan West and Hak-lok Ng

The macrobutton field can be used as a text marker within a template, or, as the name implies, it can be used to run a macro.

Using MacroButton fields as a text marker

You can use a MacroButton field that doesn't actually run a macro but simply displays a prompt and allows the user to click on the prompt and type. To do this, insert a field like:

{ MACROBUTTON NoMacro [Click here and type name] }

Change Click here and type name to whatever text you require. Press F9 to update the field, which will also display the display text instead of the field code:

See Microsoft's fax templates (which are supplied with Word) for examples of this. Also see:

How to create a template that makes it easy for users to fill in the blanks, without doing any programming.

Using {MacroButton} fields to run a macro

For this, use a field like this:

{ MACROBUTTON MyMacroName [Double-click here to run macro] }

See: Run a macro when a user double-clicks a button in the document for more details of how to create the field.

Macro button fields can make it very easy to set up fairly sophisticated templates with very little programming effort. For example, see:

Using {Macrobutton} fields to insert information from the Outlook Address Book into documents such as letters.

But they can be useful for all sorts of things – for some more examples, see:

Using hyperlinks in protected forms
Enable a user to double-click text in a document to change its value
Organizing your macros

Also see the checkboxes in the Microsoft Fax templates which are supplied with Word, where the macros associated with the fields insert AutoText entries, one AutoText entry being a MacroButton field containing a checked checkbox symbol, the other being a MacroButton field containing an unchecked one. If you copy those fields, and the macros and AutoText entries associated with them (using the Organiser) into your own templates, you can use them unmodified.

Passing arguments to MacroButton fields

Macros assigned to MacroButton fields cannot take arguments. In fact if you want to be semantic, macros cannot take arguments, ever, because a macro is defined as a public subroutine that takes no arguments, which is why subroutines that do take arguments are not shown in the list when you select Tools + Macro + Macros.

However, depending on your situation, you can get round this in a number of ways, the best two (depending on the circumstances) being.


Your macro can read the value from a Custom Document Property, or a Document Variable.


You can insert a Private field within your MacroButton field. The first thing your macro should do is look for the code of the Private field (which by definition will be the second field of the Selection) and read the value that you want to pass

This method is especially good if you have more than one MacroButton field in a single document which you want to call the same macro, but with the macro operating on a different variable in each case.

For example, you could create a nested field as follows:

{ { Private Hello world }Macrobutton TestMacro [Double-click to run macro]}

... which would display:

... and the macro could look like this:

Sub TestMacro()

MyString As String
    'Ignore first 9 characters of the private field -
    the word 'Private', and the spaces
    MyString = Mid$(Selection.Fields(2).Code, 9)
    MsgBox MyString
End Sub


Instead of a Private field, you could use an Addin field within your MacroButton field. An Addin field is very similar to a Private field but even more private - see Using Addin Fields.

Note that the order matters; the following works as one would wish it to:

{ Macrobutton TestMacro [Double-click to run macro]{ Addin }}

... but the following makes the MacroButton field's display text invisible:

{ { Addin }Macrobutton TestMacro [Double-click to run macro]}

The macro could look like this:

Sub TestMacro()

MyString As String
    MyString = Selection.Fields(2).Data
    MsgBox MyString
End Sub