Solved

Word 2010 Macro Help: Updating bookmark

Posted on 2013-01-09
7
956 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

623 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