View Single Post
 
Old 03-13-2016, 04:01 AM
highrise955 highrise955 is offline Windows 10 Office 2013
Advanced Beginner
 
Join Date: Mar 2016
Posts: 37
highrise955 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Are you sure which document the code is running against? You're using a mixture of Selection, ThisDocument and ActiveDocument calls and I can't tell from what you've posted if they're all referencing the same document.

You might want to check out: https://www.msofficeforums.com/word-...html#post87989

I utilized your code in the link you provided, minus the recommended modifications since there is only one table in my document. I do have a question about it though.

Here it is...

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
Dim tRows As Integer
Const Pwd As String = "" 'Insert password (if any) here

With CCtrl
  'Check that the Content Control is within our table.
  If .Range.InRange(ActiveDocument.Tables(1).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
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 Selection.Tables(1).Rows
    'Insert an empty paragraph after our table, then replace it with a replica of the last row
    With .Last.Range
      .Next.InsertBefore vbCr
      .Next.FormattedText = .FormattedText
    End With
    'Reset all content controls in the new last row
    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 = ""
      End With
    Next
  End With
  
' Go to first cell in new row and change the number listed.
    Selection.EndKey Unit:=wdColumn
    Selection.StartOf Unit:=wdRow
    tRows = Selection.Information(wdMaximumNumberOfRows)
    tRows = tRows - 8
    Selection.Delete
    Selection.TypeText tRows
    
  ' Re-protect the document, if applicable
  .Protect Type:=Prot, Password:=Pwd
End With
End Sub
Here is my table in question...



"Bubble" column is just a number (no CC) that needs to increment by 1 for each new row. (The way I am currently doing it is to get the number of rows and minus the number of rows in the header section of the table. (I'm sure this is inefficient but it gets the job done until I learn the "proper" way of doing it.)
"Required Dimension" is a RichText CC.
"Loc.", "Actual Dimension", and "Method of Inspection" are PlainText CC's
"Evaluation" is a PlainText CC which could just as easily be text (no CC) since the word "Pass" will always be in that cell.

I can only get your code to run if I Shift-Tab out of the Evaluation cell since it is the last CC in the document. I would prefer it to run when the user Tabs out of the "Method of Inspection" cell. Can you offer a hint on how I would do that?

I appreciate your assistance with this and my previous submission you helped me with.
Reply With Quote