How to Obtain the Generated Text of a Style

T

Tyler

I would like to know if it is possible to get the "value" of a heading that
Word is using programmatically. I will try to clarify my question with a
demonstration. If I have a document and I set it to use a numbered heading
style for its layout, I might have something that looks like:

1. Overview
2. Introduction
3. How to do it 101
3.1 Starting
3.2 Finding Help
4. Advanced Options

In this case, the leading number and period are supplied by the style, and I
simply type in the text (Overview, Introduction, etc.). When I parse this
document in VBA, I can get the style names and the text, but I'm not sure
how (if it is even possible) to get the characters Word is automatically
generating (1., 2., etc.). Outlined below is a little pseudo code to
provide an idea of where I'm currently sitting:

for each section in WordDoc.Sections
for each paragraph in section.Paragraphs
' Get the text - Overview, Introduction, etc.
text = paragraph.Range.Text

' Get the style name - Heading 1, Heading 2, etc.
styleName = paragraph.style.NameLocal

' How can I get the text Word is putting in based on the style?
<< Unknown code here >>

' Do some stuff with the information.
next
next

Any help would be much appreciated.

Thanks, Tyler
 
D

Dave Lett

Hi Tyler,

I'm not sure how it will fit into your pseudo code (i.e., "based on style"),
but I thought you might find the following useful:

With Selection.Paragraphs(1).Range.ListFormat
Debug.Print .ListString
End With

HTH,
Dave
 
T

Tyler

Thank you, that is awesome! It's exactly what I was looking for. Do you
have any good reference material/web sites you would recommend for figuring
out this sort-of information?

Thanks, Tyler
 
D

Dave Lett

Hi Tyler,

This sort of information? Well, no, I don't. I like the Word MVPs site
(http://word.mvps.org/), but I don't think this is addressed there. For this
one, I simply knew that I needed to poke around with "List" and see what I
could stumble upon.

Dave
 
K

Klaus Linke

Dave Lett said:
Hi Tyler,

This sort of information? Well, no, I don't. I like the Word MVPs site
(http://word.mvps.org/), but I don't think this is addressed there. For this
one, I simply knew that I needed to poke around with "List" and see what I
could stumble upon.

Dave


It's there in the MVP FAQs, but maybe hard to find if you don't know already
what to search for (say, ListString):
http://www.word.mvps.org/faqs/numbering/ListString.htm

Regards,
Klaus
 
K

Klaus Linke

Hi Dave,

I didn't find it easily, either. I just *knew* it was there and kept looking.
And you said you *thought* there wasn't an article, so no correction was called
for...

;-) Klaus
 

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