Suppress unwanted jumping/scrolling on Word 2013 VBA Script

后端 未结 2 842
再見小時候
再見小時候 2020-12-12 01:21

When accessing Legacy Form Field values in my Word 2013 document like this (getting or setting):

\' get
myField = ActiveDocument.FormFields(\"myField\").Resu         


        
相关标签:
2条回答
  • 2020-12-12 01:54

    This is a fantastic workaround if all you need to access is the Result field. Unfortunately, some of the form field properties are not accessible indirectly through the Bookmark collection, most notably StatusText. I am using formfields to build tests that are graded automatically in VBA code, and StatusText is the only field (to my knowledge) that allows you to store static (non-volatile) answer data in each form field.

    Here's a sample of the code I am using. The following loop tests the Result fields to see if they match the answers stored in the StatusText field

    For Each fld In oFormFields
    
        strResult = fld.Result
        strStatus = fld.StatusText
    
        If strStatus = strResult Then
            ' color answer green to show that it was correct
            fld.Range.Font.ColorIndex = wdBrightGreen
         Else
            ' color answer red to show that it was wrong
            fld.Range.Font.ColorIndex = wdRed
         End If
     Next fld 
    

    I see no way of replacing the form field references with bookmark references, but perhaps I am missing something.

    0 讨论(0)
  • 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.

    Instead of using the FormField object, access the Result property through the Bookmark object.

    Change this:

    myField = ActiveDocument.FormFields("myField").Result
    

    To this:

    myField = ActiveDocument.Bookmarks("myField").Range.Fields(1).Result
    

    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!! :-)

    0 讨论(0)
提交回复
热议问题