MacroButton


Run a macro when a user double-clicks a button in the document


Press Control+F9 to insert a set of Field Code Braces. { }.
Do not simply type the braces; you must use Control + F9.
Type the following between the braces:


MACROBUTTON MyMacroName Double-Click To Run Macro
(Use the name of an actual macro, instead of MyMacroName)
You should end up with something like this:
{ MACROBUTTON MyMacroName Double-Click To Run Macro }
3
Right-click the area between the braces and select Toggle Field Codes.
If desired, format the macro button to look like a button, using Format Font, Format Paragraph, and Format Borders and Shading



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() 
Dim 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() 
Dim MyString As String
    MyString = Selection.Fields(2).Data
    MsgBox MyString
End Sub


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

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.


For example, you may wish to include addressee details in a Letter template, without having to resort to either using a custom dialog (UserForm1) or a mail merge.


In this instance, insert the following field in the document template at the position the addressee information is to be placed:


{MACROBUTTON InsertAddressFromOutlook [Double-click here to insert address; click to type address.]}


To create the field, either:



Press Ctrl+F9 to position the field boundaries "{}" (don't type them); then type MACROBUTTON followed by the macro's name and the display prompt (you can have spaces within the prompt). Or


Select Insert + Field, and in the dialog, find the MacroButton field. This method is particularly good if you are not used to working with the field in question, because of all the help facilities the dialog gives you (see: Some Tips and "Gotchas" for those who are new to Word). The former method is quicker, though.


Then press F9 to update the field, which will also display the display text instead of the field code:



If the user single-clicks anywhere in the field, then entire field is selected:


(Note that the field shading is an option you can switch on or off under Tools + Options + View).


A single left mouse button click anywhere in the field will select the field, allowing it to be overtyped:


A double left mouse button click will run the macro attached to the field.


The macrobutton field can be used to run any macro e.g. you might wish to extract a name from your Outlook contacts list, for insertion on a template for writing company cheques:


{MACROBUTTON InsertNameFromOutlook [Double-click here to insert name; click to type name]}


... which displays:


You can use the following macros with the above fields:


Public Sub InsertAddressFromOutlook() 
    Dim strCode As String, strAddress As String
    Dim iDoubleCR As Integer

'Set up the formatting codes in strCode
    strCode = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr & _
            "<PR_COMPANY_NAME>" & vbCr & _
            "<PR_POSTAL_ADDRESS>" & vbCr

'Display the 'Select Name' dialog, which lets the user choose
'a name from their Outlook address book
    strAddress = Application.GetAddress(AddressProperties:=strCode, _
            UseAutoText:=False, DisplaySelectDialog:=1, _
            RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
'If user cancelled out of 'Select Name' dialog, quit
    If strAddress = "" Then Exit Sub

'Eliminate blank paragraphs by looking for two carriage returns in a row
    iDoubleCR = InStr(strAddress, vbCr & vbCr)
    Do While iDoubleCR <> 0
        strAddress = Left(strAddress, iDoubleCR - 1) & _
                Mid(strAddress, iDoubleCR + 1)
        iDoubleCR = InStr(strAddress, vbCr & vbCr)
    Loop

'Strip off final paragraph mark
    strAddress = Left(strAddress, Len(strAddress) - 1)
'Insert the modified address at the current insertion point
    Selection.Range.Text = strAddress

End Sub

Public Sub InsertNameFromOutlook() 
    Dim strCode As String, strName As String

'Set up the formatting codes in strCode
    strCode = "<PR_DISPLAY_NAME>"
'Display the 'Select Name' dialog, which lets the user choose
'a name from their Outlook address book
    strName = Application.GetAddress(AddressProperties:=strCode, _
            UseAutoText:=False, DisplaySelectDialog:=1, _
            RecentAddressesChoice:=True, UpdateRecentAddresses:=True)
'If user cancelled out of 'Select Name' dialog, quit
    If strName = "" Then Exit Sub

'Insert the name at the current insertion point
    Selection.Range.Text = strName
End Sub

Notes
The user may find that the first time they run the macro in each Word session, they might see the "Choose profile" dialog and have to click OK:
And/or, they may find that the "Select Names" dialog displayed by the macro doesn't automatically display their contacts, and they have to select the drop-down and select "Contacts" every time.
If your users experience either of these problems, it is down to an Outlook configuration issue, and you should post the details in the microsoft.public.outlook.contacts newsgroup.


If the document is protected (using Tools + protect Document), and if the MacroButton foeld is in a protected area of the document, you will need to unprotect the document immediately before the line that starts:


Selection.Range.Text = 

and reprotect it immediately after that line - e.g.:


    ActiveDocument.Unprotect 
    Selection.Range.Text = strAddress
    ActiveDocument.Unprotect
'Insert the modified address at the current insertion point
    Selection.Range.Text = strAddress
    ActiveDocument.Protect Type:=wdAllowOnlyFormFields, _
            NoReset:=True


Alternatively, if you did want to use a UserForm in an AutoNew macro, and wanted the UserForm to have a button that allowed your users to pick a name from the Outlook Address Book, you could use almost exactly the same code as provided in this article to do that as well. That is essentially how the Microsoft Letter Wizard works.



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext