禁止在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.
通过使用Bookmark
对象访问Result
属性,而不是使用FormField
对象.
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屋!