Auto Update Headers

N

Nic

Am self taught in VBA so please be patient with me!!

I have a code that prompts the user to insert fields into a new word doc
created from a template. The code I used is:
_________________________________________
Sub AutoNew()

Dim DocNum As String
Dim Title As String
Dim Author As String
Dim RevNo As String
Dim oStory As Range

Title = InputBox("Enter the Document Title:", "Title", "New Document")
Author = InputBox("Enter the Document Author:", "Author", "Originator's Name")
DocNum = InputBox("Enter Doc number", "Doc Number", "XXX-XXXX")
RevNo = InputBox("Enter Rev Number", "Rev Number", "Rev Number")
ActiveDocument.Variables("DocNum").Value = DocNum
ActiveDocument.Variables("RevNo").Value = RevNo
ActiveDocument.BuiltInDocumentProperties("Title").Value = Title
ActiveDocument.BuiltInDocumentProperties("Author").Value = Author

For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType < wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End Sub

Sub AutoOpen()

Dim Update As VbMsgBoxResult
Dim DocNum As String
Dim RevNo As String
Dim Title As String
Dim Author As String
Dim oStory As Range

On Error GoTo ExitSub
Update = MsgBox("Update Document Information?", vbYesNo)
If Update = vbYes Then
Title = InputBox("Enter the Document Title:")
Author = InputBox("Enter the Document Originator:")
DocNum = InputBox("Enter Doc number", "Doc Number")
RevNo = InputBox("Enter Rev Number", "Rev Number")
ActiveDocument.Variables("DocNum").Value = DocNum
ActiveDocument.Variables("RevNo").Value = RevNo
ActiveDocument.BuiltInDocumentProperties("Title").Value = Title
ActiveDocument.BuiltInDocumentProperties("Author").Value = Author

For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType < wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
End If
ExitSub:
End Sub

_________________________________

My problem is it is not updating the document (in particular the header) after
section breaks. I can manually go into the header and right click on the
field and select update field but would like this to run automatically. Any
assistance on what I should add to the above code to make this happen is much
appreciated.

Nic
 
N

Nic

Anne,

Apologies for my not understanding. I only have very basic knowledge of
programming. Your assistance is much appreciated.

I created a new module in the template and pasted your text in from below.
The following error message was displayed:
Compile Error: Expected: line number or label or statement or end of
statement.

What am I doing wrong??

Thanks again for your assistance.
Nic
 
J

Jean-Guy Marcil

Anne Troy was telling us:
Anne Troy nous racontait que :
Nic, you might just want to add some code at the end of your document
that updates all the fields for you:

Sub
UpdateAllDocFields()

Dimsry

ForEach sry
In ActiveDocument.StoryRanges

sry.Fields.Update

NextStr
End
Sub

It should be:

'_______________________________________
Sub UpdateAllDocFields()

Dim sry As StoryRange

For Each sry In ActiveDocument.StoryRanges
sry.Fields.Update
Next

End Sub
'_______________________________________

But even that will not work all the time. It will not work if you have
fields in more than one textbox, or if you have unlinked odd/even
headers/footers.

For a complete cure to those problems, see:

http://www.word.mvps.org/faqs/customization/ReplaceAnywhere.htm

and replace the code about replacing by the code for updating (from above).
(i.e. replace
SearchAndReplaceInStory rngStory, "cat", "dog"
by
rngStory.Fields.Update
and don't use the SearchAndReplaceInStory Sub.)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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