Share via

Get the table index of a content control

Anonymous
2015-01-08T13:45:28+00:00

Hi,

I have a Word document with several tables, n rows, 6 columns. The fifth column contain in each cell a content control (combobox)

Depending on the selected item, I want to color that cell. Using the following code:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

   ''' table 1 is networking, table 2 is security analysis

   Dim oRng As Word.Range

   Dim intRow As Integer, intCol As Integer

   Set oRng = ContentControl.Range

   intRow = oRng.Cells(1).RowIndex

   intCol = oRng.Cells(1).ColumnIndex

   Select Case ContentControl.Range.Text

      Case "OK"

         oRng.Cells(1).Shading.BackgroundPatternColor = -704577639

      Case "N/A"

         oRng.Cells(1).Shading.BackgroundPatternColor = -603930625

      Case Else ''' clear contents

         oRng.Cells(1).Shading.BackgroundPatternColor = -654246093

   End Select

End Sub

What I would add:

  • In case "OK", add the date into the cell on the same row, column 6.
  • For the other options, clear the contents of the cell in column 6.

I tried to figure out how to find the table index but I am get stuck. Any idea how to get the index? Thanks for your time.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Jay Freedman 207.7K Reputation points Volunteer Moderator
2015-01-08T22:12:33+00:00

Just as oRng.Cells(1) is the cell that contains the range, oRng.Tables(1) is the table that contains the range. Using that expression, you can write your code as

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)

   ''' table 1 is networking, table 2 is security analysis

   Dim oRng As Word.Range

   Dim intRow As Integer, intCol As Integer

   Dim oTbl As Word.Table

   Set oRng = ContentControl.Range

   Set oTbl = oRng.Tables(1)

   intRow = oRng.Cells(1).RowIndex

   intCol = oRng.Cells(1).ColumnIndex

   Select Case ContentControl.Range.Text

      Case "OK"

         oRng.Cells(1).Shading.BackgroundPatternColor = -704577639

         oTbl.Cell(intRow, 6).Range.Text = Format(Now, "yyyy/MM/dd")

      Case "N/A"

         oRng.Cells(1).Shading.BackgroundPatternColor = -603930625

         oTbl.Cell(intRow, 6).Range.Text = ""

      Case Else ''' clear contents

         oRng.Cells(1).Shading.BackgroundPatternColor = -654246093

         oTbl.Cell(intRow, 6).Range.Text = ""

   End Select

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2015-01-09T12:22:27+00:00

    Hi Jay,

    Thank you for this nice solution. I had found a workaround by adding tags to the comboboxes but this is much better. I'll adapt my code.

    Was this answer helpful?

    0 comments No comments