Paragraph Number and Text accessed from a Table?

J

Joe HM

Hello -

I have a loop that steps through all tables of a document ...

For Each lTable In ActiveDocument.Tables
...
Next lTable

How can I determine the Paragraph Number and Paragraph Text that
contains the table?

Here is an example ...

3.1 Blah 1
Text ...
Table 1
More Text ...

3.2 Blah 2
Text ...
Table 2
More Text ...

I need to extract the "3.1" and "Blah 1" when lTable is Table 1 in the
loop above.

Is there an easy way to do that?

Thanks!
Joe
 
J

Jonathan West

I'm not sure what you mean. A table contains paragraphs and text, not the
other way round. Do you mean the number and text of the heading immediately
above the table?
 
J

Joe HM

Hello -

Sorry I was not clear enough. Yes ... I need to loop through all
tables and determine which section they are contained in ... as you
said ... the heading immediately above the table.

Thanks,
Joe
 
J

Jonathan West

Joe HM said:
Hello -

Sorry I was not clear enough. Yes ... I need to loop through all
tables and determine which section they are contained in ... as you
said ... the heading immediately above the table.

Try this


Dim oTable as Table
Dim oHeading as Range

For Each oTable in ActiveDocument.Tables
oTable.Range.Select
Set oHeading =
ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs.First.Range
Debug.Print oHeading.ListFormat.ListString, oHeading.Text
Next oTable

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
J

Joe HM

Awesome ... thanks a lot!

I am trying to understand what this code is doing and wondering how I
could access the next-highest heading information - e.g. table is
contained in 3.2.1 and I would like to get the heading text of 3.2?

Thanks!
Joe
 
J

Jonathan West

Joe HM said:
Awesome ... thanks a lot!

I am trying to understand what this code is doing and wondering how I
could access the next-highest heading information - e.g. table is
contained in 3.2.1 and I would like to get the heading text of 3.2?

They key to this is understanding what the \HeadingLevel bookmark does.
Wherever the current selection is, the \HeadingLevel bookmark stretches from
the heading paragraph above the selection (or including the selection, if
the selection is itself in a heading) to just above the next heading of the
same level.

Therefore, if you have found a third-level heading, and you want to find
it's parent 2nd-level heading, all you need do is position the selection
just in front of the start of the \Headinglevel bookmark, and then see where
it now reaches. It will wither reach to the previous 3rd-level heading, or
to the parent 2nd-level heading. You repeat this process until you reach the
2nd-level heading.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top