如何正确访问Word 2013 VBA中的旧表单字段复选框? [英] How to properly access Legacy Form Field checkboxes in Word 2013 VBA?
问题描述
来自这个问题我想尝试获取和设置遗留表单字段复选框(MS Word 2013)的值 - 没有奇怪的滚动/跳跃在文档上。对于文本字段,可以通过
Coming from this question I am trying to get and set the values for a Legacy Form Field checkbox (MS Word 2013) – without weirdly scrolling/jumping around on the document. For Text Fields this can be performed by
myField = ActiveDocument.Bookmarks("myField").Range.Fields(1).Result ' get
ActiveDocument.Bookmarks("myField").Range.Fields(1).Result.Text = myValue ' set
但这不适用于复选框。
But this doesn't work for checkboxes. What commands can I take in order to access checkboxes accordingly and without weird jumping around?
推荐答案
我做了一些测试,并且,它看起来你可以简单地使用 CheckBox.Value
属性来访问该复选框的值,而不改变文档中的焦点。将以下代码粘贴到常规模块中,并测试这两种访问文本框值的方式之间的区别。
I did some testing on this, and it looks you can simply use the CheckBox.Value
property to access the value of the checkbox without changing the focus in your document. Paste the following code into a regular module, and test the difference between these two ways of accessing the value of the text box.
Public Sub TestCheckboxAccess()
Dim ctl As FormField
For Each ctl In ActiveDocument.FormFields
' Loop through check boxes in current document
If ctl.Type = wdFieldFormCheckBox Then
' This does not scroll the document
Debug.Print ctl.CheckBox.Value
' This does scroll the document
'Debug.Print ctl.Result
End If
Next ctl
End Sub
请注意,在我的系统上,此代码示例即使不关闭 ScreenUpdating
也可以工作。
Note that on my system this code sample works even without turning off ScreenUpdating
.
尝试一下,看看是否可以解决这个问题!
Give this a try, and see if it might solve the problem for you!
这篇关于如何正确访问Word 2013 VBA中的旧表单字段复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!