Macro to detect Header text in Section 3 of document

E

Ed

I need a routine to determine the presence of text in the various "stories"
in my document. If text is present, I work on the text. If no text is
present, I can skip processing in the story.
In particular (and where the below routine seems to fall apart), I want to
see if a header contains text. The below works fine if there is header text
in Section 1 of the document, but if there is no header text in sections 1
and 2, but there is header text section 3, the routine won't find it. (The
MakeHFValid routine is used, although not reprinted, to overcome the problem
when a story is blank. Does this code make sense to anyone, and if so, can
you tell me why header text in section3 does not return 'positive'? (No
even/odd page headers; no firstpage headers, etc. Just plain stuff) Thanks.

Ed

For Each osection In ActiveDocument.Sections
For Each astory In ActiveDocument.StoryRanges
MakeHFValid
aval = astory.Text
atype = astory.StoryType
If Len(aval) = 1 Then
aval = Asc(aval)
End If
If astory.StoryType = wdFootnotesStory Then
If InStr(InStory, "FNotes") = 0 And aval <> 13 Then InStory=
InStory+ "FNotes" ' the "If Instr(InStory,"FNotes") = 0" is just to
prevent a second entry of the same thing.
ElseIf astory.StoryType = wdEndnotesStory Then
If InStr(InStory, "ENotes") = 0 And aval <> 13 Then InStory=
InStory+ "ENotes"
ElseIf astory.StoryType = wdCommentsStory Then
If InStr(InStory, "Comments") = 0 And aval <> 13 Then InStory=
InStory+ "Comments"
ElseIf astory.StoryType = wdTextFrameStory Then
If InStr(InStory, "TextBox") = 0 And aval <> 13 Then InStory=
InStory+ "TextBox"
ElseIf astory.StoryType = wdEvenPagesHeaderStory Then
If InStr(InStory, "EPHead") = 0 And aval <> 13 Then InStory=
InStory+ "EPHead"
ElseIf astory.StoryType = wdPrimaryHeaderStory Then
If InStr(InStory, "Header") = 0 And aval <> 13 Then InStory=
InStory+ "Header"
ElseIf astory.StoryType = wdEvenPagesFooterStory Then
If InStr(InStory, "EPFoot") = 0 And aval <> 13 Then InStory=
InStory+ "EPFoot"
ElseIf astory.StoryType = wdPrimaryFooterStory Then
If InStr(InStory, "Footer") = 0 And aval <> 13 Then InStory=
InStory+ "Footer"
ElseIf astory.StoryType = wdFirstPageFooterStory Then
If InStr(InStory, "FPFoot") = 0 And aval <> 13 Then InStory=
InStory+ "FPFoot"
ElseIf astory.StoryType = wdFirstPageHeaderStory Then
If InStr(InStory, "FPHead") = 0 And aval <> 13 Then InStory=
InStory+ "FPHead"
End If
Next aStory
If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPrintView
Else
ActiveWindow.View.Type = wdPrintView
End If
Next oSection
 
J

Jezebel

First: there's a basic misunderstanding about sections and storyranges:
ActiveDocument.StoryRanges includes everything in all sections -- so you
don't need the outer loop.

Second: StoryRanges are linked lists. Iterating the StoryRanges collection
gets the first item in each list. Some storyranges (like Main) can have only
one item in the list, but others (like headers, footers, textboxes) can have
many. You need to use the StoryRange's NextStoryRange property to get the
next item in the list.

To iterate all content in the document --

Dim pRange as Word.Range
For each pRange in ActiveDocument.StoryRanges

Do
... do whatever

set pRange = pRange.NextStoryRange
Loop until pRange is nothing

Next
 
E

Ed

Jezebel said:
To iterate all content in the document --

Dim pRange as Word.Range
For each pRange in ActiveDocument.StoryRanges

Do
... do whatever

set pRange = pRange.NextStoryRange
Loop until pRange is nothing

Next

Jezebel,

Thanks. That is helpful. Let me give that a shot. I certainly is shorter
that what I had composed.

Ed
 

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