For example, where the tables of interest are bookmarked as Tbl1, Tbl2, and Tbl3, respectively:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
'The following code conditionally adds a new row, with content controls, to the designated table.
Dim i As Long, j As Long, Prot As Variant, StrBkMk As String
Const Pwd As String = "" 'Insert password (if any) here
'Bookmarking the table provides the flexibility of being able to deal with the addition/deletion
' of other tables before the one we want to process.
With CCtrl
'Check that the Content Control is within our bookmarked range.
' One could test for a particular table instead
If (.Range.InRange(ActiveDocument.Bookmarks("Tbl1").Range) = False) And _
(.Range.InRange(ActiveDocument.Bookmarks("Tbl2").Range) = False) And _
(.Range.InRange(ActiveDocument.Bookmarks("Tbl3").Range) = False) Then Exit Sub
'Get the number of ContentControls in the table
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
StrBkMk = .Range.Tables(1).Range.Bookmarks(1).Name
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 .Bookmarks(StrBkMk).Range.Tables(1)
'Insert an empty paragraph after our table, then replace it with a replica of the last row
With .Rows.Last.Range
.Next.InsertBefore vbCr
.Next.FormattedText = .FormattedText
End With
.Range.Bookmarks.Add (StrBkMk)
'Reset all content controls in the new last row
For Each CCtrl In .Rows.Last.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
' Re-protect the document, if applicable
.Protect Type:=Prot, Password:=Pwd
End With
End Sub