Thread: [Solved] Tables with form fields
View Single Post
 
Old 11-12-2013, 02:27 PM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,342
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try your document with the following code:
Code:
Option Explicit
Dim bLastCell As Boolean
 
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
With Selection
  If .Information(wdWithInTable) = True Then
    With ActiveDocument.Tables(2).Range
    If Selection.Cells(1).RowIndex = .Cells(.Cells.Count).RowIndex Then
      If Selection.Cells(1).ColumnIndex = .Cells(.Cells.Count).ColumnIndex Then
        bLastCell = True
      End If
    End If
    End With
  End If
End With
End Sub
 
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim CCtrl As ContentControl, Prot As Long, Pwd As String, i As Long, j As Long
If bLastCell = True Then
  If Selection.Tables(1).Range = ActiveDocument.Tables(2).Range Then
    If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
      If ActiveDocument.ProtectionType <> wdNoProtection Then
        Pwd = "" 'Insert password here
        Prot = ActiveDocument.ProtectionType
        ActiveDocument.Unprotect Password:=Pwd
      End If
      ContentControl.Range.Select
      Selection.SplitTable
      ContentControl.Range.Select
      Selection.Tables(1).Rows.Last.Range.Characters.Last.Next.InsertBefore vbCr
      With Selection.Tables(1).Rows
        .Last.Range.Copy
        .Last.Range.Next.Paste
        For Each CCtrl In .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 = ""
            .LockContentControl = True
          End With
        Next
        .First.Range.Characters.First.Previous.Delete
      End With
      ActiveDocument.Protect Type:=Prot, Password:=Pwd
    End If
  End If
  bLastCell = False
End If
Application.ScreenUpdating = True
End Sub
The principal issues that had to be dealt with here are:
1. you're working with the second table in the document, whereas the example had only one; and
2. your table has vertically-split cells (which makes it impossible to work with a row designation.

PS: I note you now have a mix of content controls and formfields in your document. You really should use one or the other - they are not designed to work together.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote