Solved

Word 2010 Macro Help: Updating bookmark

Posted on 2013-01-09
7
857 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
  • 5
  • 2
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Bevos
Comment Utility
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
Comment Utility
Interesting. Theory says that it should, but I'll test it out.
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now