Get a Word Table Row Number from a Content Control in VBScript
Question details
Determine the table row containing a Word content control and read text outside the control in the same cell.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A VBScript process reads content controls in a Word document and needs their table row numbers and surrounding cell text.
- Observed behavior
- The script needs to identify whether each control is in a table and access the cell range separately from the content-control range.
Open the Word document through your existing VBScript automation and obtain a content control object named cc. The examples use Word's numeric constant 12 for wdWithInTable.
Check the content-control range and read its table row
Use the content control's range to detect a table, get the first containing cell's row index, and read text before and after the control.
In the VBScript code that processes cc, test cc.Range.Information(12) before accessing its cells: If cc.Range.Information(12) Then rowNumber = cc.Range.Cells(1).RowIndex Else rowNumber = 0 End If RowIndex is one-based; the example uses 0 to indicate that the control is not in a table.
Use controlText = cc.Range.Text to read the text inside the control and controlTitle = cc.Title to read its title.
Inside the table branch, use the cell and document ranges to read text before and after the control: Set cell = cc.Range.Cells(1) Set doc = cc.Range.Document beforeText = doc.Range(cell.Range.Start, cc.Range.Start).Text afterText = doc.Range(cc.Range.End, cell.Range.End - 1).Text This example assumes the control is contained within one cell.

Work with Word documents in WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its Microsoft Office format support helps you move existing files into WPS; it does not fix Microsoft Word's VBScript automation.

Frequently Asked Questions
How can I tell whether a content control is inside a Word table?
Check cc.Range.Information(12). The numeric value 12 represents wdWithInTable; only access cc.Range.Cells when the check is true.
Does RowIndex start at zero or one?
Word's Range.Cells(1).RowIndex returns a one-based row index. You can use 0 as your own sentinel when the control is not in a table.
Why doesn't cc.Range.Text include the rest of the cell?
cc.Range.Text reads the content-control range, not the whole cell. Use cc.Range.Cells(1).Range to access the containing cell, or use document ranges before and after the control to isolate surrounding text.
Does reading a cell range add extra characters?
A Word cell range includes end-of-cell characters. When extracting text, account for the cell marker and any final paragraph mark rather than treating them as ordinary content.




