Search code examples
excelvbams-wordform-fields

Retrieve Row of Form Field in MS Word Table using VBA


I have a MS Word Table with FormFields in it. I am looping through the FormFields in the Document and I'd like to retrieve the Row to find adjacent cell text. Suggestions or thoughts are welcome.

With WordDoc
j = 0
For Each FField In WordDoc.FormFields
    With FField
        Select Case .Type
            Case Is = wdFieldFormTextInput, wdFieldFormDropDown
                'strResult = .Result
            Case Is = wdFieldFormCheckBox
                If FField.Name Like "*_n" Then
                      If FField.CheckBox.Value = True Then
                        'I Need Table Row from this Form Field
                      End If
                    End If
                End If
                'strResult = .CheckBox.Value
        End Select
    End With
    j = j + 1
    'wkSht.Cells(i, j) = strResult
Next
End With

Final Solution (within the True If statement):

Dim ffRow As Integer
ffRow = FField.Range.Information(wdEndOfRangeRowNumber)


If FField.Range.Information(wdWithInTable) Then
   unusedRow = wkSht.Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 0).row
   wkSht.Cells(unusedRow, 2).Value = FField.Range.Tables(1).Rows(ffRow).Cells(1).Range.Text
   wkSht.Cells(unusedRow, 3).Value = FField.Range.Tables(1).Rows(ffRow).Cells(5).Range.Text
End If

Solution

  • You can try:

    FField.Range.Information(wdEndOfRangeRowNumber)
    

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdinformation?view=word-pia