Using Macrobutton to insert address from Outlook contacts into MS

H

Huisie

I have 2 questions regarding the above:

I want to insert the following into a Word template using a Macrobutton:

"Company Name" OR "Display Name"
"Postal Address"
(open line)
"Phone No"
"Fax No"

Using the excellent examples from Graham Mayor
(http://www.gmayor.com/Macrobutton.htm) and supplementary info from MVPS.org
(http://word.mvps.org/FAQs/TblsFldsFms/InsertAdrsWMacroFld.htm), I came up
with the following:

Public Sub InsertAddressFromOutlook()
Dim strCode, strAddress As String

'Set up the formatting codes in strCode
strCode = strCode & "<PR_COMPANY_NAME>" & vbCr
strCode = strCode & "<PR_POSTAL_ADDRESS>" & vbCr & vbCr
strCode = strCode & "{TEL: {<PR_OFFICE_TELEPHONE_NUMBER>}}" & vbCr
strCode = strCode & "{FAX: {<PR_BUSINESS_FAX_NUMBER>}}"
'strCode = strCode & "<PR_LOCALITY>, <PR_STATE_OR_PROVINCE>
<PR_POSTAL_CODE>" & vbCr
'strCode = strCode & "<PR_COUNTRY>" & vbCr

'Let the user choose the name in Outlook
strAddress = Application.GetAddress("", strCode, False, 1, , , True, True)

'Strip off final paragraph mark
strAddress = Left(strAddress, Len(strAddress) - 1)

'Insert the modified address at the current insertion point
Selection.Range.Text = strAddress

'Insert the modified address at the current insertion point
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect
ActiveDocument.FormFields("NameOfFormfield").Result = strAddress
Else
Selection.TypeText strAddress
End If
End Sub


Question 1
-------------
I can't create the "OR" option in VBA.
With reference to Slipstick.com
(http://www.slipstick.com/contacts/insword.htm), this is enabled via typing
"|" between two adress fields.
When I type this option into my macro code, it all goes pear-shaped...


Question 2
-------------
You'll notice that I included 2 options for:
"Insert the modified address at the current insertion point"
They both work fine - is there any benefit using the seemingly more complex
2nd option?


Thanks in advance for any help and especially to those who posted and
allowed me to cut 'n paste their examples.
Would have been lost in the wilderness without your generosity.
 
P

Peter Jamieson

When you use the "pipe" for OR, you need to wrap the expression in {}, e.g.

strCode = strCode& "{<PR_COMPANY_NAME>|<PR_DISPLAY_NAME>}"& vbCr

etc. That works here, anyway. The code fails without the {} because
strAddress is set to "" and the following statement fails.
Question 2
-------------
You'll notice that I included 2 options for:
"Insert the modified address at the current insertion point"
They both work fine - is there any benefit using the seemingly more complex
2nd option?
If ActiveDocument.ProtectionType<> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect
ActiveDocument.FormFields("NameOfFormfield").Result = strAddress

This stuff is only really relevant if you are using Word "Online form
fields" in your document. If you don't need that, use option 1.


Peter Jamieson

http://tips.pjmsn.me.uk
 
H

Huisie

Hi, Peter

Apologies for not replying sooner.

Thank you for your time and informative answers.

I did figure out the answer to question 2 (but not the why), but was stumped
on the first.

I've been trying to perfect a template and have felt like someone missing
the last piece of a complex jigsaw.

This last piece is greatly appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top