Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Paragraph is in a Table or not in MS-Word macro?

Tags:

ms-word

vba

The paragraph object in the Word has a property called Range. Within this Range object has a property called Cells.

For paragraph that are not in a table, this property Paragraph.Range.Cells is set to "". This can be seen in the Watches window in debug mode.

For paragraph that are in a table, the property Paragraph.Range.Cells has other properties in it, for example it has a property called Count.

I am using this property of Paragraph.Range.Cells to determine if the paragraph is in a table or not. However, I cant seem to figure out how to test this.

For example, I cannot simply test like this...

If paragraph.Range.Cells <> Null Then.... or even If IsNull(paragraph.Range.Cells) Then ...

It throws a Run-time error '5907' There is no table at this location

So, how would I test for this? thanks

like image 629
Dhaliwal Avatar asked Jun 22 '11 08:06

Dhaliwal


1 Answers

You can use the Information property:

If Selection.Information(wdWithInTable) Then
  'What ever you'd like to do
End If

Hence you don't need any manual error catching mechanisms.

like image 66
Philipp Sternberg Avatar answered Sep 29 '22 21:09

Philipp Sternberg