user78090
user78090

Reputation: 161

Find the table that a paragraph is in

I am using VBA to read through a Microsoft Word docx using code like this:

For Each P In WD.Paragraphs 
    Temp = P.Range.Text
    If P.Range.Information(wdWithInTable) Then
    'Other stuff done here
Next P

There are a number of tables mixed into the documents and it reads the cells of them as paragraphs. I want to get the rows and columns of whatever table the current paragraph is in. Ideally, that would be a reference to the table itself. I also know you can iterate through tables like this:

For Each T In WD.Tables
    Rcount = T.Rows.Count
    Ccount = T.Columns.Count
    'temp = T.Cell(R, C).Range.Text 'can get individual cell info like this
Next T

My first thought was to compare the text of the paragraph to the text of the table cells to try and find a match, but the cell may be empty so it's not a sure thing. I'm not able to find an answer by searching; possibly because word, paragraph, and table are such common terms.

Upvotes: 1

Views: 1073

Answers (1)

user78090
user78090

Reputation: 161

Tim's method correctly finds a reference to the table:

Set oTable = P.Range.Tables(1)

or for just the rows and columns:

R = P.Range.Tables(1).Rows.Count
C = P.Range.Tables(1).Columns.Count

Upvotes: 2

Related Questions