How to properly access Legacy Form Field checkboxes in Word 2013 VBA?

做~自己de王妃 提交于 2019-12-13 05:21:50

问题


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

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