Fields in header of protected document

M

Mike W

I am trying to automate a word form so that the
information placed in various forms on the first page is
automatically copied to the header of any additional
pages. The form has to be protected.

I have tried setting up reference fields in page 2
headers and added a macro with the only line being

ActiveDocument.fields.update

I have set up a later field to run the macro upon entry.

When I try it I can tell the macro is running but the
reference fields are not updated.

Any suggestions.

Thanks
Mike W
 
P

Peter Hewett

Hi Mike

The problem is that the line of code:

ActiveDocument.fields.update

defaults to the MainTextStory, this is the main body text of your document.
Word has 11 different story types, the fields you want to update are in one
of the 3 Header stories.

The following code will update the fields in all Headers:

Sub UpdateFieldsInHeaders()
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only update fields in a header
If Story.StoryType = wdPrimaryHeaderStory Or _
Story.StoryType = wdFirstPageHeaderStory Or _
Story.StoryType = wdEvenPagesHeaderStory Then

' Update fields in this header
Story.Fields.Update

' Update fields in any linked headers
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Update fields in this header
rngNext.Fields.Update

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next
End Sub

I hope this helps + Cheers - Peter
 
M

Mike W

Thanks,

That worked perfect.

Mike
-----Original Message-----
Hi Mike

The problem is that the line of code:

ActiveDocument.fields.update

defaults to the MainTextStory, this is the main body text of your document.
Word has 11 different story types, the fields you want to update are in one
of the 3 Header stories.

The following code will update the fields in all Headers:

Sub UpdateFieldsInHeaders()
Dim Story As Variant
Dim rngNext As Word.Range

' Iterate through all story types
For Each Story In ActiveDocument.StoryRanges

' Only update fields in a header
If Story.StoryType = wdPrimaryHeaderStory Or _
Story.StoryType = wdFirstPageHeaderStory Or _
Story.StoryType = wdEvenPagesHeaderStory Then

' Update fields in this header
Story.Fields.Update

' Update fields in any linked headers
Set rngNext = Story.NextStoryRange
Do Until rngNext Is Nothing

' Update fields in this header
rngNext.Fields.Update

' Link to next story (if any)
Set rngNext = rngNext.NextStoryRange
Loop
End If
Next
End Sub

I hope this helps + Cheers - Peter



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top