问题
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?
回答1:
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
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!
来源:https://stackoverflow.com/questions/26903616/how-to-properly-access-legacy-form-field-checkboxes-in-word-2013-vba