0
votes

I'm trying to update all the fields in a Word document using the "Fields.Update"-Method. This is the code I use:

Sub UpdateFields()

Dim varRange As Range

'Update fields in the first section of the main text
set varRange = ThisDocument.StoryRanges(wdMainTextStory)
varRange.Fields.Update

'Update fields in subsequent sections (of the main text)
While Not (varRange.NextStoryRange Is Nothing)
    Set varRange = varRange.NextStoryRange
    varRange.Fields.Update
    Wend

End Sub

There are over 750 fields in the document (linking to cell content in an/one Excelfile) so the update takes a while so for the user it looks like the application "freezes". When I update "manually" (selecting via Crtl+A and updating via F9), I get a progress bar like this within the status bar at the bottom of the word application window:

status line at the bottom of the word application window

However it does not appear using the "Fields.Update"-Methode. Is there a way to show this Bar using the Filds.Update-Method or any other Method?

I suppose I could make my own "progress"-bar via a userform. But this would require to update each field individually (e.g. through cycling through all fields indevidually rngCurrentRange.Fields(i).Update) but I would rather avoid that. It makes the code more complex, slower and it's a pain in the ass to make sure Word keeps the userform updated in real time. But if there's no other solution I'll take that as well.

EDIT: I found a workaround, but it's not working 100%, please see my answer...

Please Note: I also posted two other questions within this context:

1
One approach would be to put up a progress bar which updates by one increment for each story range. The progress bar should also advise the user that the update will take several minutes.freeflow
Freeflow's suggestion would be one possibility. Another could be to use SendKeys for the keyboard shortcut so that Word's progress bar displays. Another would be a message at the beginning informing the user that this takes a long time (possibly with the number of fields to be updated and a time estimate) so that the user knows to go get a cup of coffee. (Maybe with an image of that cup of coffee, steaming...).Cindy Meister
@Freeflow, unfortunately, I need the progress bar within a story rage. I decided to update each field separately, but I can't get the user form to update in real time. Here's the questionAlbin

1 Answers

0
votes

I found a workaround using Fields.Update in combination with selection:

'Do Field Updates 
Dim rngCurrentRange As Range
set varRange = ThisDocument.StoryRanges(wdMainTextStory)
rngCurrentRange.Select
Selection.Fields.Update

But there is still a problem with this workaround: At first it doesn't seem to show a progress bar either, but when you run it a second time it does! I'm not sure why this is, but it made it possible to make a workaround: First I update some insignificant StoryRange, which will initialize the progress bar for the following Fields.Update-methods. Unfortunately this does not work for the "first approach" (without using selection).