How to step through Headings in .DOC

R

Rich

I need to be able to step through each Heading in a Word
document.

Would someone have a code sample?
 
J

Jonathan West

Hi Rich

Moving to the next heading is as simple as this

Selection.Move Unit:=wdHeading, Count:=1

Put that it a loop. The only problem is that it doesn't give you an obvious
way of knowing when you have reached the end. To check for this, save the
Selection.End property before you do the move, and compare its value after
the move. If no change, then you are at the last heading.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
D

Dave Lett

Hi Rich,

What do you mean by "step through"? And what do you want to do with the
heading? For example, you can get the name of each heading by using the
GetCrossReferenceItems method, but if you actually want to modify, say, the
paragraph, then this method might not be the best choice.

HTH
 
D

Dave Lett

Hi Rich,

I think the following will suit your purpose:

With Selection
.EndKey Unit:=wdStory
.GoTo What:=wdGoToHeading, Which:=wdGoToPrevious
MsgBox .Style 'or the code to return your "level
End With

HTH
 
R

Rich

I'm running PP 2000 -- I don't see the wdHeading constant
mentioned in the help file anywhere. Actually, I can't
find a reference to that constant at MSDN, either.
 
J

Jonathan West

Rich said:
I'm running PP 2000 -- I don't see the wdHeading constant
mentioned in the help file anywhere. Actually, I can't
find a reference to that constant at MSDN, either.

Sorry, my mistake. Should be

Selection.GoToNext What:=wdGoToHeading


--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
K

Klaus Linke

I just need to find the level of the last heading in a
document.

Or if you mean the last heading above the current selection:

Dim myRange As Range
Set myRange = Selection.Range.Duplicate
myRange.GoTo What:=wdGoToBookmark, _
Name:="\HeadingLevel"
myRange.Collapse (wdCollapseStart)
MsgBox myRange.Paragraphs(1).Range.Text, _
, myRange.ParagraphFormat.OutlineLevel

Regards,
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