Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word 2010 Macro Help: Updating bookmark

Posted on 2013-01-09
7
Medium Priority
?
976 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

660 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