View Single Post
 
Old 09-07-2017, 03:02 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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

Even without bookmarking the table, your code should be able to work out which table it's being run from. For example, suppose you're using content controls with a ContentControlOnExit macro:
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim i As Long, j As Long
With CCtrl
  'The following code conditionally adds a new row, with content controls, to the current table.
  If .ShowingPlaceholderText = True 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
  'Solicit user input
  If MsgBox("Add new row?", vbQuestion + vbYesNo) <> vbYes Then Exit Sub
  With .Range
    i = .Cells(1).RowIndex
    With .Tables(1).Range
      j = .Cells(.Cells.Count).RowIndex
    End With
  End With
  With .Range.Rows(1).Range
    If i = j Then
      'Insert an empty paragraph after our table
      .Characters.Last.Next.InsertBefore vbCr
    Else
      'temporarily split our table, which inserts an empty paragraph after it
      .Characters.Last.Next.InsertBreak wdColumnBreak
    End If
    'replace the empty paragraph after our table with a replica of the last row
    .Characters.Last.Next.FormattedText = .FormattedText
  End With
  i = i + 1
  'Initialize the content controls in our new row
  Call InitializeCCtrls(.Range.Tables(1), i)
End With
Application.ScreenUpdating = True
End Sub

Sub InitializeCCtrls(Tbl As Table, r As Long)
Dim CCtrl As ContentControl
'Reset all content controls in the designated row to the initial state
With Tbl.Rows(r).Range
    For Each CCtrl In .ContentControls
      With CCtrl
        Select Case .Type
          Case wdContentControlCheckBox: .Checked = False
          Case wdContentControlRichText, wdContentControlText, wdContentControlDate: .Range.Text = ""
        Case wdContentControlDropdownList
          .Type = wdContentControlText
          .Range.Text = ""
          .Type = wdContentControlDropdownList
        Case wdContentControlComboBox
          .Type = wdContentControlText
          .Range.Text = ""
          .Type = wdContentControlComboBox
        End Select
      End With
    Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote