M
mysteryg45
I need to create a letter template for work that pulls information
from the Outlook address book. I don't know much about VBA, but I was
able to get enough information to pull this together:
' *** 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(7) As String
' Populate the array of address codes
astrCodes(0) = ""
astrCodes(1) = "<PR_GIVEN_NAME>"
astrCodes(2) = "<PR_SURNAME>"
astrCodes(3) = "<PR_POSTAL_ADDRESS>"
astrCodes(4) = "" 'Subject
astrCodes(5) = "<PR_GIVEN_NAME>"
astrCodes(6) = "" 'Initials
astrCodes(7) = "" 'File name and path
'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
' *** Code End
Now, I want to change the template so it has a header on all pages
after the first, which also pulls from the address book, but I can't
figure out how to make it work. Adding more astrCodes doesn't seem to
do anything.
Any suggestions would be much appreciated.
from the Outlook address book. I don't know much about VBA, but I was
able to get enough information to pull this together:
' *** 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(7) As String
' Populate the array of address codes
astrCodes(0) = ""
astrCodes(1) = "<PR_GIVEN_NAME>"
astrCodes(2) = "<PR_SURNAME>"
astrCodes(3) = "<PR_POSTAL_ADDRESS>"
astrCodes(4) = "" 'Subject
astrCodes(5) = "<PR_GIVEN_NAME>"
astrCodes(6) = "" 'Initials
astrCodes(7) = "" 'File name and path
'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
' *** Code End
Now, I want to change the template so it has a header on all pages
after the first, which also pulls from the address book, but I can't
figure out how to make it work. Adding more astrCodes doesn't seem to
do anything.
Any suggestions would be much appreciated.