Re: VBA Template Form



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" <lgabbaian@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!


.



Relevant Pages

  • Re: VBA Template Form
    ... Dim strAddress As String ... 'a name from their Outlook address book. ... For Each fldMyField In ActiveDocument.Fields ... to a header on all pages after the first. ...
    (microsoft.public.word.vba.beginners)
  • Re: programmtically read internet header in c#
    ... Actually iam using outlook 2003. ... Iam capturing the selection event ... selectionchange.In that i have to wirte a c# code to read the email header. ...
    (microsoft.public.office.developer.com.add_ins)
  • Re: OFFICE TEXT SELECTION
    ... Milly Staples [MVP - Outlook] ... the (insert latest virus name here) virus, all mail sent to my personal ... Even if you don't have a microphone plugged in Speech ... An unwanted selection begins at the blinking ...
    (microsoft.public.office.misc)
  • Re: Get a list of all the toolbars currently displaying in Outlook
    ... ItemAdd event in the folder Junk Suspects and you would think the argument ... Then I use the Selection Collection to select that added ... Dim oeExplorer As Outlook.Explorer ... Keep your Outlook categories organized! ...
    (microsoft.public.outlook.program_vba)
  • RE: Table Question
    ... I modified the last macro from ‘How can I prevent users from editing the ... a user tries to open the header it goes to frmTable.Show. ... Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection) ... Create a WindowSelectionChange that fires every time the selection is ...
    (microsoft.public.word.vba.general)