How to find the name of the section of a document that holds a tab

D

Don

I have successfully created a macro that copies all tables in a word document
to an excel document (each table to a new worksheet).

There is one more thing I want to do - name the excel worksheets after the
section of the document the table came from. The idea is to find the string
that for the document heading (e.g. table of contents entry) that contains
the table, and use that to name my worksheet.

I'm fairly proficient the Excel VBA but just starting with word. I've gone
through the word object model, but nothing looks obvious to me.

I don't need code written for me ... I just need to be pointed in the right
direction. Right now I just have no clue what to look for.
 
J

Jean-Guy Marcil

Don was telling us:
Don nous racontait que :
I have successfully created a macro that copies all tables in a word
document to an excel document (each table to a new worksheet).

There is one more thing I want to do - name the excel worksheets
after the section of the document the table came from. The idea is to
find the string that for the document heading (e.g. table of contents
entry) that contains the table, and use that to name my worksheet.

I'm fairly proficient the Excel VBA but just starting with word.
I've gone through the word object model, but nothing looks obvious to
me.

I don't need code written for me ... I just need to be pointed in the
right direction. Right now I just have no clue what to look for.

Where exactly should this information be picked up? Would there be a
heading/title directly preceding the table? A legend? Further up in the
document?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Don

Jean-Guy Marcil said:
Where exactly should this information be picked up? Would there be a
heading/title directly preceding the table? A legend? Further up in the
document?
"Further up" I guess. I'm looking for the nearest autonumbered heading (not
list ... just headings )

I was kind of hoping that the table object would have a property that tells
me this, but it does not appear to be so.
 
J

Jean-Guy Marcil

Don was telling us:
Don nous racontait que :
"Further up" I guess. I'm looking for the nearest autonumbered
heading (not list ... just headings )

I was kind of hoping that the table object would have a property that
tells me this, but it does not appear to be so.

You can't really expect an unknown paragraph be linked to a table...
Tables in the the Word object model are part of a collection, normally
indexed in the order in which they appear in the document.

In your case, are you looking for the nearest preceding numbered paragraph,
regardless of the outline level? Are all tables part of a document that
contains outline numbering?
Can you show us some representative samples of what you are dealing with?

You could have code that scans the paragraphs preceding a table until a
specific one is found. The key is using the appropriate condition for
determining if the right paragraph has been found.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Don

Jean-Guy Marcil said:
In your case, are you looking for the nearest preceding numbered paragraph,
regardless of the outline level? Yes
Are all tables part of a document that contains outline numbering? Yes
Can you show us some representative samples of what you are dealing with?
Not at the moment ... the document is at work and I am at home.
You could have code that scans the paragraphs preceding a table until a
specific one is found. The key is using the appropriate condition for
determining if the right paragraph has been found.
Yes - that sounds good. Having an instance of a table object, it is not
clear to me how to find preceding paragraphs, nor what I should look for in a
paragraph. Is there some property of a paragraph that says it is a numbered
heading?
 
J

Jean-Guy Marcil

Don was telling us:
Don nous racontait que :
Not at the moment ... the document is at work and I am at home.
Yes - that sounds good. Having an instance of a table object, it is
not clear to me how to find preceding paragraphs, nor what I should
look for in a paragraph. Is there some property of a paragraph that
says it is a numbered heading?

You declare a Range
You Set a range to a table
Use the Range object to get to previous Paragraphs
Loop until you find what you want

For example, crudely... (You would need additional code to make sure that
you are not trying to access a paragraph before the first one in the
document and you would want to loop through all tables in the document...)


Dim rgeTable As Range
Dim strHeading As String

Set rgeTable = ActiveDocument.Tables(1).Range

Do While rgeTable.ListFormat.ListType <> wdListOutlineNumbering
Set rgeTable = rgeTable.Paragraphs(1).Previous.Range
If rgeTable.ListFormat.ListType = wdListOutlineNumbering Then
'Remove the ¶ from the Range
rgeTable.MoveEnd wdCharacter, -1
strHeading = rgeTable.Text
End If
Loop

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Don

Jean-Guy Marcil said:
Don> You declare a Range
You Set a range to a table
Use the Range object to get to previous Paragraphs
Loop until you find what you want

For example, crudely... (You would need additional code to make sure that
you are not trying to access a paragraph before the first one in the
document and you would want to loop through all tables in the document...)


Dim rgeTable As Range
Dim strHeading As String

Set rgeTable = ActiveDocument.Tables(1).Range

Do While rgeTable.ListFormat.ListType <> wdListOutlineNumbering
Set rgeTable = rgeTable.Paragraphs(1).Previous.Range
If rgeTable.ListFormat.ListType = wdListOutlineNumbering Then
'Remove the ¶ from the Range
rgeTable.MoveEnd wdCharacter, -1
strHeading = rgeTable.Text
End If
Loop
This looks good. I'll give it a try. Thanks.
 
D

Don

Jean-Guy Marcil said:
You declare a Range
You Set a range to a table
Use the Range object to get to previous Paragraphs
Loop until you find what you want

For example, crudely... (You would need additional code to make sure that
you are not trying to access a paragraph before the first one in the
document and you would want to loop through all tables in the document...)


Dim rgeTable As Range
Dim strHeading As String

Set rgeTable = ActiveDocument.Tables(1).Range

Do While rgeTable.ListFormat.ListType <> wdListOutlineNumbering
Set rgeTable = rgeTable.Paragraphs(1).Previous.Range
If rgeTable.ListFormat.ListType = wdListOutlineNumbering Then
'Remove the ¶ from the Range
rgeTable.MoveEnd wdCharacter, -1
strHeading = rgeTable.Text
End If
Loop
After adding a bit of logic to limit the string length and regex out any
special characters (excel limits worhsheet name to 31 chars and does not like
:\/?*[]) it worked lik a charm. Thanks!
 

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