Albin
Albin

Reputation: 1110

VBA (Word): force user form to update in real time

I have a word file (Word 2016) with approx. 750 fields. Through a VBA macro I update each field separately (.Fields(i).Update) in order to be able to make a "poor men's" progress bar showing the user the status of the update (how many fields have been updated and how many fields there are in total):

'Select Storyrange (first section)
Dim rngRange as Range
rngRange = ThisDocument.StoryRanges(wdMainTextStory)
Dim intFields as Integer
intFields = rngRange.Fields.Count

'Show Form
UserForm1.Show vbModeless

'Update each field individually
For i = 1 To intFields 
    'Update Field
    rngRange.Fields(i).Update
    'Update User Form
    UserForm1.Label1.Caption = i & "/" & intFields 
Next i

The problem is, that the user form does not get updated in real time. The first few counts work (approx. until i = 20), then there user form doesn't update (approx. until i = 150), after that the update works again. I already tried:

use Repaint and DoEvents

[snip]
'Update User Form
UserForm1.Label1.Caption = i & "/" & intFields 
Repaint
DoEvents
Application.ScreenRefresh 'just to be save, since - in my view - ScreenRefresh only effects the Word window, not the user form
[snip]

use a separate sub

[snip]
'Update User Form
updateUserform i, intFields
[snip]

Dim updateUserform(i as Integer, intFields as Integer)
    UserForm1.Label1.Caption = i & "/" & intFields 
    Repaint
    DoEvents
End Sub

So my question is: are there any other option to force a user form update?

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

Upvotes: 0

Views: 1207

Answers (1)

Albin
Albin

Reputation: 1110

Seems in some case it is a Windows/Office glitch. Restarting Office/Windows solved the problem in some (but not all) cases when I encountered the problem.

Upvotes: 0

Related Questions