 |
|
|
|
 |
Using {MacroButton} fields to insert information from the Outlook Address
Book into documents such as letters
|
Article contributed by Graham Mayor
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
|
1.
|
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.
|
|
2.
|
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
|
__________________
|
1.
|
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.
|
|





|