Solved

Word 2010 Macro Help: Updating bookmark

Posted on 2013-01-09
7
928 Views
Last Modified: 2013-01-13
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?
0
Comment
Question by:Bevos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38759357
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

0
 

Author Comment

by:Bevos
ID: 38760358
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?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38761010
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bevos
ID: 38763099
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38763443
Interesting. Theory says that it should, but I'll test it out.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38763609
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).
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38765417
It seems that there has been a bug in StoryRanges since the Word object model was created for Word 97. Ranges in text boxes in headers or footers are missed out. There are various workarounds on other forums. This is mine.
Sub UpdateAllFields(doc As Word.Document)
    Dim rng As Word.Range
    Dim hdr As HeaderFooter
    Dim r As Integer
    Dim sh As Shape
    Dim ilsh As InlineShape
    
    For Each rng In doc.StoryRanges
        r = 0
         Do
            r = r + 1
            rng.Fields.Update
            Select Case rng.StoryType
                Case wdEvenPagesHeaderStory To wdFirstPageFooterStory 'any sort of header or footer
                    'look in 'floating' shapes
                    For Each sh In rng.ShapeRange
                        If sh.TextFrame.HasText Then
                            sh.TextFrame.TextRange.Fields.Update
                        End If
                    Next sh
                    'look in inline shapes
                    For Each ilsh In rng.InlineShapes
                        ilsh.Range.Fields.Update
                    Next ilsh
            End Select
            Set rng = rng.NextStoryRange
        Loop Until rng Is Nothing
    Next rng
End Sub

Open in new window

0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question