I'm working on a Word template (form) that contains tables having a mix of formfields such as textinput, dropdowns, and checkboxes. I would like to provide a single default data row for each table, but if the user enters valid data in the first cell of the last row, the table expands with a new default row below the active cell the user just filled in. The following code is latest draft for a table where default textinput for cell(1) is "nn", but requires at least two data rows and is not smart enough to know if another default row is available below the active (selected) cell:Sub AddRow()
Dim thisDoc As Document
Set thisDoc = ActiveDocument
Dim oTable As Table
Dim oCell As Cell
Dim oPrevRow As Row, oNewRow As Row
Dim tmp
'Unprotect Document
If thisDoc.ProtectionType <> wdNoProtection Then
thisDoc.Unprotect
End If
tmp = Selection.Text
If tmp <> "nn" Then
If tmp <> "n" Then
'Selection.FormFields(1).TextInput.Clear
' Copy last row
Set oTable = thisDoc.Tables(10)
Set oPrevRow = oTable.Rows(oTable.Rows.Count)
oPrevRow.Range.Copy
Set oNewRow = oTable.Rows(oTable.Rows.Count)
' Copy last row to new row
oNewRow.Range.Paste
End If
End If
'Protect Document
If thisDoc.ProtectionType = wdNoProtection Then
thisDoc.Protect wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
I also would like to ensure that the formfields in the new row are reset to their default values without resetting the entire form.