抑制 Word 2013 VBA 脚本上不需要的跳跃/滚动 [英] Suppress unwanted jumping/scrolling on Word 2013 VBA Script
问题描述
像这样访问 Word 2013 文档中的旧表单字段值时(获取或设置):
When accessing Legacy Form Field values in my Word 2013 document like this (getting or setting):
' get
myField = ActiveDocument.FormFields("myField").Result
' set
ActiveDocument.FormFields("myField").Result = myValue
文档奇怪地上下跳跃/滚动并停在一个完全不同的位置(它似乎跳到了所引用字段所在的行).
the document weirdly jumps/scrolls down and up and stops at a complete different position (it seems to jump to the lines where the referred fields are positioned).
Have a look at this screencast or a sample file where the error can be viewed.
我用过
Application.ScreenUpdating = False
在我的Sub
和
Application.ScreenUpdating = True
在我的 Sub
末尾,但不幸的是这没有帮助.
at the end of my Sub
but unfortunately this doesn't help.
我需要修改什么才能抑制这种行为?
What do I have to modify in order to suppress this behaviour?
推荐答案
我在 Word MVP 网站.就像您指出的那样,问题是当您访问 FormField
对象的属性时,焦点设置在此表单字段上.即使在 VBA 编辑器中通过本地窗口浏览表单字段时,您也可以看到这种行为.
I found a clue to the solution on the Word MVP Site. Like you pointed out, the issue is when you access the properties of a FormField
object, the focus is set to this form field. You can see this behavior even when browsing the form fields through the Locals Window in the VBA editor.
不要使用 FormField
对象,而是通过 Bookmark
对象访问 Result
属性.
Instead of using the FormField
object, access the Result
property through the Bookmark
object.
改变这个:
myField = ActiveDocument.FormFields("myField").Result
为此:
myField = ActiveDocument.Bookmarks("myField").Range.Fields(1).Result
现在您无需更改文档中的焦点即可访问此字段的值.要在字段上设置值,您可以使用 Text
属性.
Now you can access the value of this field without changing the focus in your document. To set a value on a field, you can use the Text
property.
ActiveDocument.Bookmarks("myField").Range.Fields(1).Result.Text = myValue
希望这有帮助!!:-)
Hope this helps!! :-)
这篇关于抑制 Word 2013 VBA 脚本上不需要的跳跃/滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!