Re: VBA Template Form
- From: mysteryg45 <lgabbaian@xxxxxxxxx>
- Date: Wed, 26 Nov 2008 10:48:50 -0800 (PST)
Thanks Doug. I feel like I'm getting on the right track -- It does
get the addressee's name in the header of page 2 and beyond. However,
it adds a couple of other problems.
First, it adds a copy of the addressee's name and address to the top
of the letter, which would be fine except that I don't know how to
move it around to the place I would need it to be, and the code I
already has it in the necessary spot.
Second, it overwrites all the other information I had in the header
that didn't need to be accessed by VBA to work in the previous version
of the template (meaning it would be just a blank macro in the old
version...information like page number and date).
To help clarify, and maybe make it easier for someone to help, I've
posted a copy of the actual template (it will be up for 7 days):
http://www.yousendit.com/download/TTZuc0wycWY1UjQwTVE9PQ
And here's the VBA code with Doug's suggestion included:
' *** 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(6) 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
'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
Dim Addressee As String
Selection.Text = Application.GetAddress(UseAutoText:=False,
DisplaySelectDialog:=1, RecentAddressesChoice:=True,
UpdateRecentAddresses:=True)
Addressee = Selection.Paragraphs(1).Range.Text
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Range.Text = Addressee
End With
' Clean up memory
strCode = ""
strAddress = ""
Set fldMyField = Nothing
Is there another addition I can make? Or am I just going about the
whole project incorrectly?
Thanks in advance for any help!
On Nov 24, 1:12 am, "Doug Robbins - Word MVP"
<d...@xxxxxxxxxxxxxxxxxx> wrote:
The following will insert the selected name and address of the selected
contact from Outlook at the location of the selection and the name of the
selected contact in the primary header of the first Section of the document,
setting the Different First Page attribute of the Page Setup for that
Section to true so that the addressee's name only appears in the header of
the second and subsequent pages.
Dim Addressee As String
Selection.Text = Application.GetAddress(UseAutoText:=False, _
DisplaySelectDialog:=1, RecentAddressesChoice:=True, _
UpdateRecentAddresses:=True)
Addressee = Selection.Paragraphs(1).Range.Text
With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterPrimary).Range.Text = Addressee
End With
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
"mysteryg45" <lgabba...@xxxxxxxxx> wrote in message
news:69cb0142-bb5b-42d9-89ea-b1eb2b1e8dd0@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Nov 21, 12:45 pm, Jean-Guy Marcil
<JeanGuyMar...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
"mysteryg45" wrote:
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.
it is not entirely clear exactly what it is you want to do now.
What will the exact content of that 2nd page header be?
How does it relate to the information you get with your code above?
Just in case it's not entirely clear, this is how the above code
works: When the user opens a new doc from this template, they are
prompted to choose a name from the Outlook address book. The person's
name and address are then applied to different parts of the document
(a letter). As long as I stay within the normal body of the page, the
above code works fine.
Now I've discovered I need to add information, including the
addressee's name, to a header on all pages after the first. But when
I try to apply this code to the document's header, it doesn't do
anything. It just ignores the blank macro.
Is there something specific I have to add to make the code work in the
header/footer of the document? Also, will it matter that I've split
the document into sections to distinguish the first page header from
subsequent pages'?
Is there a better way to accomplish what I'm trying to do?
Thanks again!- Hide quoted text -
- Show quoted text -
.
- Follow-Ups:
- Re: VBA Template Form
- From: Doug Robbins - Word MVP
- Re: VBA Template Form
- References:
- VBA Template Form
- From: mysteryg45
- RE: VBA Template Form
- From: Jean-Guy Marcil
- Re: VBA Template Form
- From: mysteryg45
- Re: VBA Template Form
- From: Doug Robbins - Word MVP
- VBA Template Form
- Prev by Date: Re: VBA Template Form
- Next by Date: Re: VBA Template Form
- Previous by thread: Re: VBA Template Form
- Next by thread: Re: VBA Template Form
- Index(es):
Relevant Pages
|