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

半世苍凉 提交于 2020-01-22 02:43:31

问题


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:

  • Update multiple fields with Excel links very slow
  • How to show the progress of the “Fields.Update”-Method in VBA (Word)

回答1:


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.



来源:https://stackoverflow.com/questions/57125330/vba-word-force-user-form-to-update-in-real-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!