Google Answers Logo
View Question
 
Q: Microsoft Word 2000 - Custom Insertion of Address in Document ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Microsoft Word 2000 - Custom Insertion of Address in Document
Category: Computers
Asked by: dyermaker-ga
List Price: $35.00
Posted: 30 May 2003 13:35 PDT
Expires: 29 Jun 2003 13:35 PDT
Question ID: 210869
I need to be able to insert address information into a Word document
from the Outlook address book. (all Office 2000)  I can do this with
the existing insert address button.  However, I want to insert
specific fields in certain places in the document.  I know I can edit
the default "addresslayout" autotext.  That will not help me.

It should work like the existing Fax Wizard, where it draws the name
and fax number from an Address Book entry and inserts in the correct
place.  But I do not want the complexity of the Fax Wizard, and I want
to use my own templates.

Our company currently has this working in WordPerfect 8, using the
address book there, but we need to discontinue that and move to Word. 
There are macro buttons set up then when pressed, run the macro that
prompts for an address which can be manually entered or drawn from an
address book, then it inserts the proper data in the fields of the
template.

How can I get this to work in Word?  It can be done with macros,
visual basic, or whatever it takes.  I need to understand how it
works, so I can apply it in different ways.
Answer  
Subject: Re: Microsoft Word 2000 - Custom Insertion of Address in Document
Answered By: hammer-ga on 03 Jun 2003 11:48 PDT
Rated:5 out of 5 stars
 
Excellent step-by-step instructions with sample code for doing this
can be found at the Microsoft Word MVP FAQ site.

Using {MacroButton} fields to insert information from the Outlook
Address Book into documents such as letters
http://www.mvps.org/word/FAQs/TblsFldsFms/InsertAdrsWMacroFld.htm

The technique illustrated in the article uses two built-in abilities
of Word.

1. {MacroButton} fields: These are field which can run a macro if
double-clicked.
For more information, Word Help has a page on MacroButton. 

2. GetAddress: A Word function that can retirieve information from the
default address book. If you open the VBA Editor, type GetAddress, and
press F1, you will get the Help page for GetAddress complete with
examples.

This looks like the most flexible and adaptable method for your
requirements. Please ask for clarification if I can help you find
additional details on this.

Search Strategy:
Word insert outlook address document

Good luck with your project!

- Hammer

Request for Answer Clarification by dyermaker-ga on 04 Jun 2003 06:25 PDT
This gets me closer to something I can use, as far as understanding
MACROBUTTON better.  But the key is placing different fields from the
same address book entry in different places on the document, with one
macro, (or button or whatever.)  The Fax Wizard does this, but I can't
extract the proper steps.

To clarify what I mean, the answer provided allows me to insert an
address, in whatever layout I want, within the document, all in one
field.  I need to run a macro that will put the name in one field, the
company name in another, the fax number in another, etc.  The Fax
Wizard does that, but just with Name and Fax Number.  I need to be
able to customize to work with any of my templates and with any of the
address book fields.

Clarification of Answer by hammer-ga on 04 Jun 2003 06:43 PDT
Let me look into the techniques for iterating through the existing
Fields in a Word document template. I'll get back to you shortly.

- Hammer

Clarification of Answer by hammer-ga on 04 Jun 2003 14:10 PDT
OK, how about this?

I created a new Template called test.dot. I typed some text into my
template. Everywhere that I wanted a piece of address information to
appear, I pressed Ctrl-F9 to get an empty field which looks like {}.
My template looks like this:

This is a template where I want the information to go after I’m done
selecting.
The name of the person is {} and their address is 
{}. Their surname, {}, is another thing we know.

I opened the Visual Basic Editor using Alt-F11. Under my Template
Project, I right-clicked on "ThisDocument" and selected View Code.
(Make sure you don't accidentally edit the Normal template instead of
yours.)

I added two routines to the code module. One inserts the address
information. The other uses the New event of the document to select an
address every time the template is used. The code is heavily
commented. Please ask for clarification if you need anything
explained. Note: No lines of code should wrap. You may need to unwrap
lines after pasting this into your code editor.

' *** Code Begin

Option Explicit

Private Sub Document_New()

    InsertAddressFromOutlook

End Sub

Private Sub InsertAddressFromOutlook()
Dim strCode As String
Dim strAddress As String
Dim fldMyField As Field
' Declare an array to hold the field code.
' Arrays index from 0, so to fill three
' fields, we declare...
Dim astrCodes(2) As String

    ' Populate the array of address codes
    astrCodes(0) = "<PR_GIVEN_NAME>"
    astrCodes(1) = "<PR_POSTAL_ADDRESS>"
    astrCodes(2) = "<PR_SURNAME>"


    'Display the 'Select Name' dialog, which lets the user choose
    'a name from their Outlook address book. Since this is the first
    'call to GetAddress, DisplaySelectDialog = 1.
    strAddress = Application.GetAddress(UseAutoText:=False,
DisplaySelectDialog:=1, RecentAddressesChoice:=True,
UpdateRecentAddresses:=True)
    'If user cancelled out of  'Select Name' dialog, quit
    If strAddress = "" Then Exit Sub
    
    'For each field, use the matching value from astrCodes
    'to get the correct address value. Note that in these
    'calls to GetAddress, DisplaySelectDialog = 2. This
    'tells Word to use the address that was selected previously.
    For Each fldMyField In ActiveDocument.Fields
        strAddress = ""
        strCode = astrCodes(fldMyField.Index - 1)
        strAddress =
Application.GetAddress(AddressProperties:=strCode, _
                UseAutoText:=False, DisplaySelectDialog:=2)
        fldMyField.Result.Text = strAddress
    Next fldMyField

' Clean up memory
strCode = ""
strAddress = ""
Set fldMyField = Nothing

End Sub

' *** Code End

Additional Information:
A complete list of the Outlook formatting codes like <PR_SURNAME> on
the Word Help page for the AddAddress Method.

The InsertAddressFromOutlook routine can be easily updated for
different number of fields by adjusting the declaration and population
of astrCodes.

Please let me know how this works out for you.

Best regards,

- Hammer

Clarification of Answer by hammer-ga on 04 Jun 2003 14:12 PDT
Oh, after running, the document looks like this:

This is a template where I want the information to go after I’m done selecting.
The name of the person is John and their address is 
1234 Redwood Court
Somewhere, AL 11111. Their surname, Smith, is another thing we know.


- Hammer

Request for Answer Clarification by dyermaker-ga on 05 Jun 2003 08:13 PDT
I know I've already rated your answer, but I tried to implement this
in my Fax template and I'm having a problem.  I need five different
fields to be filled, so I've changed the code as I thought was
necessary.

Dim fldMyField As Field 
' Declare an array to hold the field code. 
' Arrays index from 0, so to fill three 
' fields, we declare... 
Dim astrCodes(4) As String 
 
    ' Populate the array of address codes 
    astrCodes(0) = "<PR_GIVEN_NAME>" 
    astrCodes(1) = "<PR_POSTAL_ADDRESS>" 
    astrCodes(2) = "<PR_SURNAME>"
    astrCodes(3) = "<PR_SURNAME>"
    astrCodes(4) = "<PR_SURNAME>"

Are these the only changes that needed to be made?  I'm getting an
error when I run it,

"Run Time Error '9'
Subscript out of range"

on this line:

strCode = astrCodes(fldMyField.Index - 1)

Does it have anything to do with the non-blank field I have later in
the document? (For user name, initials, data, etc...)

Let me know if you feel I need to post this as an additional question.

Thanks.

Clarification of Answer by hammer-ga on 05 Jun 2003 08:41 PDT
For this to work as is, you must have the same number of fields as you
have elements in your array. If that is not the case in your template,
you will have to write conditional code to tell it which ones to skip.
Alternately, you can always put the same number of elements in your
array as you have fields, the set that array element to "". Then, wrap
the GetAddress stuff in a check for If strCode <> "".

You are getting the subscript out of range error because the
Field.Index = 7, so it is asking for element 6 from an array with only
5 elements.

- Hammer

Request for Answer Clarification by dyermaker-ga on 05 Jun 2003 10:40 PDT
I figured it had to do with the number of fields in the document not
matching the array.  I don't understand the

"Then, wrap the GetAddress stuff in a check for If strCode <> ""."

Where's that go and what does it do?

Clarification of Answer by hammer-ga on 05 Jun 2003 11:13 PDT
Here is how the routine would look if you have 10 fields in your
document, but only 7 of them were to be populated from a selected
address.

Private Sub InsertAddressFromOutlook() 
Dim strCode As String 
Dim strAddress As String 
Dim fldMyField As Field 
' Declare an array to hold the field code. 
' Arrays index from 0, so to fill ten 
' fields, we declare... 
Dim astrCodes(9) As String 
 
    ' Populate the array of address codes 
    ' Note the 3 codes that are set to a
    ' zero-length string ("").
    astrCodes(0) = "<PR_GIVEN_NAME>" 
    astrCodes(1) = "<PR_POSTAL_ADDRESS>" 
    astrCodes(2) = "<PR_SURNAME>" 
    astrCodes(3) = "" 
    astrCodes(4) = "<PR_SURNAME>" 
    astrCodes(5) = "<PR_SURNAME>" 
    astrCodes(6) = "<PR_SURNAME>" 
    astrCodes(7) = "" 
    astrCodes(8) = "" 
    astrCodes(9) = "<PR_SURNAME>" 
 
 
    'Display the 'Select Name' dialog, which lets the user choose 
    'a name from their Outlook address book. Since this is the first 
    'call to GetAddress, DisplaySelectDialog = 1. 
    strAddress = Application.GetAddress(UseAutoText:=False,
DisplaySelectDialog:=1, RecentAddressesChoice:=True,
UpdateRecentAddresses:=True)
    'If user cancelled out of  'Select Name' dialog, quit 
    If strAddress = "" Then Exit Sub 
     
    'For each field, use the matching value from astrCodes 
    'to get the correct address value. Note that in these 
    'calls to GetAddress, DisplaySelectDialog = 2. This 
    'tells Word to use the address that was selected previously. 
    For Each fldMyField In ActiveDocument.Fields 
        strAddress = "" 
        strCode = astrCodes(fldMyField.Index - 1)
        ' If the array element is a zero-length string
        ' then skip that field
        If strCode <> "" Then 
            strAddress =
Application.GetAddress(AddressProperties:=strCode, _
                UseAutoText:=False, DisplaySelectDialog:=2) 
            fldMyField.Result.Text = strAddress
        End If
    Next fldMyField 
 
' Clean up memory 
strCode = "" 
strAddress = "" 
Set fldMyField = Nothing 
 
End Sub 


- Hammer

Request for Answer Clarification by dyermaker-ga on 05 Jun 2003 12:06 PDT
Great.  Thank You.  It works now, and I think I even understand it.

Clarification of Answer by hammer-ga on 05 Jun 2003 12:38 PDT
Great! That's what we're after... :)

- Hammer

Request for Answer Clarification by dyermaker-ga on 05 Jun 2003 13:23 PDT
I've posted a follow up question for you that I think is outside the
scope of this one.  Please see:
http://answers.google.com/answers/main?cmd=threadview&id=213567

Clarification of Answer by hammer-ga on 05 Jun 2003 13:31 PDT
I see it.

- Hammer
dyermaker-ga rated this answer:5 out of 5 stars
Excellent Work.  Clarification provided me with the exact solution I
was looking for.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy