Link to home
Start Free TrialLog in
Avatar of Bevos
Bevos

asked on

Word 2010 Macro Help: Updating bookmark

I have several bookmark fields in a document (BrandName, Title, ReportNumber, Revised Date, OriginalDate) which are referenced in various places I want to update them via a macro.  My macro is working really well for the majority of the document but these bookmark fields in the header are always unaffected after I run the macro.  

The macro I am using is very simple:
Sub UpdateFields()
On Error Resume Next
    ActiveDocument.StoryRanges(wdMainTextStory).Fields.Update
    ActiveDocument.StoryRanges(wdPrimaryFooterStory).Fields.Update
    ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields.Update
End Sub

Could anyone please help me find a solution?
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

This code walks through each instance of each story type.
Sub UpdateAllFields(doc As Word.Document)
    Dim rng As Word.Range
    
    For Each rng In doc.StoryRanges
        Do
            rng.Fields.Update
            Set rng = rng.NextStoryRange
        Loop Until rng Is Nothing
    Next rng
End Sub

Open in new window

Avatar of Bevos
Bevos

ASKER

Thanks Graham, but this one isn't showing up in my list of Macros in Word 2010 (View -> Macros).  I tried removing the text in ( ) after the sub and it will show up.  Thoughts?
That's because it needs a parameter.
You can call it from a modified version of your original sub
Sub UpdateFields()
     UpdateAllFields ActiveDocument
End Sub

Open in new window

Avatar of Bevos

ASKER

Hi Graham now I can definitely run the update script but it works much like before.  The header area doesn't 'update' automatically.  It is required that I right click and select 'update field' from the menu to get the updated value to populate the reference.  If it makes a difference these elements are contained in a small table in the header area.

Thanks again for all the help,
Bev
Interesting. Theory says that it should, but I'll test it out.
It works in my tests, buy they could be too simple. Could you post a small sample document, please?

I am using Time fields (with seconds).
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial