Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

Macro to detect Header text in Section 3 of document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 24 Apr 2006 23:32 GMT
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
Jezebel - 25 Apr 2006 01:09 GMT
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

>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
[quoted text clipped - 58 lines]
> End If
> Next oSection
Ed - 25 Apr 2006 02:46 GMT
> To iterate all content in the document --
>
[quoted text clipped - 8 lines]
>
> Next

Jezebel,

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

Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.