Document Map

G

Guest

I want to extract from a document all the headers and at least 2 lines from each paragraph of normal text in a hierarchical manner in the style of the document map. I want to extract this information to a database. I am using the following code
Dim prg As Paragrap
Dim sty As Styl

For Each prg In ActiveDocument.Paragraph
Set sty = prg.Styl
If sty.Type = wdStyleTypeParagraph Then
If prg.Style <> wdStyleNormal Then
Debug.Print "Style: " & prg.Style & vbCrLf & " Title: " & prg.Range.Tex
Next pr

When i run this code i seem to be getting pretty much all the styles from my document, but i only want the header styles and in the hierarchy they come in the document ie Header 1, Header 2, Header 2, Header 3, Header 2 etc...
 
D

Dave Lett

Hi jat,

Instead of cycling through the paragraph collection, which takes a long
time, you might consider using the GetCrossReferenceItems method. Have a
look at the following and see if it might suit your needs.

Dim sHeadings
Dim iHeading As Integer
Dim sHeadingText As String
Selection.HomeKey Unit:=wdStory
sHeadings =
ActiveDocument.GetCrossReferenceItems(ReferenceType:=wdRefTypeHeading)
For iHeading = 1 To UBound(sHeadings)
With Selection
With .Find
.Text = Trim(sHeadings(iHeading))
.ClearFormatting
.Execute
End With
.MoveEnd Unit:=wdLine, Count:=3
sHeadingText = sHeadingText & Selection.Text & vbCrLf
.MoveRight
End With
Next iHeading

Debug.Print sHeadingText

HTH,
Dave
jat said:
I want to extract from a document all the headers and at least 2 lines
from each paragraph of normal text in a hierarchical manner in the style of
the document map. I want to extract this information to a database. I am
using the following code:
Dim prg As Paragraph
Dim sty As Style

For Each prg In ActiveDocument.Paragraphs
Set sty = prg.Style
If sty.Type = wdStyleTypeParagraph Then _
If prg.Style <> wdStyleNormal Then _
Debug.Print "Style: " & prg.Style & vbCrLf & " Title: " & prg.Range.Text
Next prg

When i run this code i seem to be getting pretty much all the styles from
my document, but i only want the header styles and in the hierarchy they
come in the document ie Header 1, Header 2, Header 2, Header 3, Header 2
etc...
 

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