A family of Microsoft word processing software products for creating web, email, and print documents.
For code to add a new row to any table in a document with content controls try:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
'The following code conditionally adds a new row, with content controls, to the current table.
Dim i As Long, j As Long, r As Long, Prot As Variant
Const Pwd As String = "" 'Insert password (if any) here
'Exit if we're not in a table.
If CCtrl.Range.Information(wdWithInTable) = False Then Exit Sub
With CCtrl
i = .Range.Tables(1).Range.ContentControls.Count
'Get our ContentControl's index # in the table
j = ActiveDocument.Range(.Range.Tables(1).Range.Start, .Range.End).ContentControls.Count
'Check that we're using the last content control
If i <> j Then Exit Sub
End With
'Solicit user input
If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
With ActiveDocument
' Un-protect the document, if applicable
Prot = .ProtectionType
If .ProtectionType <> wdNoProtection Then
Prot = .ProtectionType
.Unprotect Password:=Pwd
End If
With CCtrl.Range
r = .Cells(1).RowIndex
With .Tables(1)
If r = .Rows.Count Then
'Insert an empty paragraph after our table, then replace it with a replica of the last row
.Rows.Last.Range.Next.InsertBefore vbCr
Else
.Split .Rows(r + 1)
End If
.Rows.Last.Range.Next.FormattedText = .Rows(r).Range.FormattedText
'Reset all content controls in the new row
For Each CCtrl In .Rows(r + 1).Range.ContentControls
With CCtrl
If .Type = wdContentControlCheckBox Then .Checked = False
If .Type = wdContentControlRichText Or .Type = wdContentControlText Then .Range.Text = ""
If .Type = wdContentControlDropdownList Then .DropdownListEntries(1).Select
If .Type = wdContentControlComboBox Then .DropdownListEntries(1).Select
If .Type = wdContentControlDate Then .Range.Text = ""
End With
Next
End With
End With
' Re-protect the document, if applicable
.Protect Type:=Prot, Password:=Pwd
End With
End Sub
With the above code, you'll be prompted as to whether you want to add a new row when you exit the last content control in a given table. The new row, if added, will have all content controls cleared.