Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro to process header?

Status
Not open for further replies.

benpollinger

Technical User
Oct 4, 2002
43
GB
Hello all,

I have a Word 97 template for a simple report on an individual.

The fields are 'Name', 'Surname' and 'DOB' (date of birth)

I have set a macro as follows using ASK and FILLIN fields, which stores the inputs into bookmarks.

As per the Word help file, I have made a macro to prompt for these inputs when a new document is made. The macro is simply:

Sub AutoNew()
Selection.WholeStory
Selection.Fields.Update
Selection.HomeKey Unit:=wdStory
End Sub

This works fine, but I would like to use the fields in a header. This macro does not update headers, and I cannot record selecting and updating header contents in by using 'record macro'.

Can anyone suggest macro code that would do this? I don't really know VBA, but all it would need (I think) is to add these actions to the above:

view header
select header contents
update fields
view main
home

Slightly complicated by the fact that the first page has no header, i.e. the First Page header is empty.

Thanks everyone (anyone?!),
Ben
 
No you can't record selecting and updating the header (and have it do any good), but you can record making a header from scratch and then breakdown that code and make it work for you. I mostly use Excel and have only made one VBA project in Word and it was to build an interface to quickly make headers in 200 different Word documents. I just recorded making a header and then looked at the code for awhile then away I went making it work for me.

Good Luck Ben, wish I could provide the code I used but it was a while back and I don't know where it is.

Charles
Walden's Machine, Inc.
Quality/Office Developer
 
Hiya,

this might get you started:
[/code]
Dim l_docHeaderFooter As Document

Set l_docHeaderFooter = ActiveDocument
With l_docHeaderFooter
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "This is my header"
.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "This is my footer"
End With
set l_docHeaderFooter = nothing[/code]

Also you might take a look in the object model / VBA Help for the HeaderFooter object & collection

HTH

Cheers
Nikki
 
A belated thanks for your replies - I will try these out and post back here with code if I can get it to work!

cheers,
Ben
 
Cracked it!

Below is the code needed to do what I was asking about. It uses "Application.Browser.Next" to go to the second page before viewing the header, because my document has a different first page header.

This only works if the Browse Object is Page, hence the line "Application.Browser.Target = wdBrowsePage"

You could remove those lines if you had one unchanging header.

Cheers again for your help, Charles & Nikki,
Ben
--

Code:
Sub AutoNew()
    Selection.WholeStory
    Selection.Fields.Update
    Selection.HomeKey Unit:=wdStory
    Application.Browser.Target = wdBrowsePage
    Application.Browser.Next
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.WholeStory
    Selection.Fields.Update
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Selection.HomeKey Unit:=wdStory
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top