How to loop thru a document at each level

G

Guest

Hello!

Does anyone have an example macro that can loop thru each of the various
levels of a WORD document down to the WORD level? ie similar to the following
pseudocode?

For Each oSection In ActiveDocument.Sections
for each oPara in oSection.Item
for each oSentence in oPara.Item
for each oWword in oSentence.Item

If aword.HighlightColorIndex = wdYellow Then
MsgBox
(oSection.Text(1)+oPara.Text(1)+oSentence.Text(1)+aword)
End If

next oWord
Next OSentence
Next oPara
Next oSection

Basically, what i'm trying to create is a 'notes taken' index with hyperlnks
back to the notes that i've hightlighted, so when i run the macro, i'll end
up with a sub document of the highlighted text plus the 1st sentence in the
Section, the first sentence in the paragraph ect.

Thanks very much for any examples on how to do this!
BobK
 
G

Guest

got it:

iSectCount = ActiveDocument.Sections.Count
For i = 1 To iSectCount
iParCount = ActiveDocument.Sections(i).Range.Paragraphs.Count
hsect = ActiveDocument.Sections(i).Range.Text
hsect = Left$(hsect, 20)
For J = 1 To iParCount
iSent = ActiveDocument.Paragraphs(J).Range.Sentences.Count
hpara = ActiveDocument.Paragraphs(J).Range.Text
hpara = Left$(hpara, 20)
For k = 1 To iSent
iWord =
ActiveDocument.Paragraphs(J).Range.Sentences(k).Words.Count
hsent = ActiveDocument.Paragraphs(J).Range.Sentences(k).Text
hsent = Left$(hsent, 20)
If iWord > 0 Then
For l = 1 To iWord
'MsgBox ("w:" +
ActiveDocument.Paragraphs(J).Range.Words(l).Text)
If
ActiveDocument.Paragraphs(J).Range.Sentences(k).Words(l).HighlightColorIndex
= wdYellow Then
hword =
ActiveDocument.Paragraphs(J).Range.Sentences(k).Words(l).Text
MsgBox ("s: " + hsect + " p:" + hpara + " s:" + hsent
+ " w:" + hword)
Write #1, hsect, hpara, hsent, hword
End If
Next l
End If
Next k
Next J
Next i
 

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