Albin
Albin

Reputation: 1120

How to show the progress of the "Fields.Update"-Method in VBA (Word)

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:

Upvotes: 0

Views: 696

Answers (1)

Albin
Albin

Reputation: 1120

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).

Upvotes: 0

Related Questions