« Oracle Forms goodness - extending the text fields | Main | Updated PHP guide »

September 19, 2006

MS Word - VBA - StoryRanges - Odd-page headers

Daily trivia:
Document.StoryRanges does not explicitly return odd-page headers/footers. Instead, they're linked from the even-page ones. One has to use Range.NextStoryRange on the even-page headers/footers to get the odd-page ones.

Because example code is always nice, here's my function to grab _all_ of the ranges in a document, updated with this knowledge:


' Builds a collection of all the ranges in this document.
' (StoryRanges, which include the headers and footers, augmented with any text box ranges)
' also include linked ranges via NextStoryRange to grab the odd page header/footers
Private Function AllRanges(Optional Doc As Document) As Collection
Dim Rng As Range
Dim Sh As Shape
Set AllRanges = New Collection
If Doc Is Nothing Then Set Doc = ActiveDocument
For Each Rng In Doc.StoryRanges
While Not Rng Is Nothing
AllRanges.Add Rng
Set Rng = Rng.NextStoryRange
Wend
Next Rng
' Grab all of the text boxes in the document, too.
' Use .ContainingRange to return the entire story that flows between linked text frames,
' but examine .Previous to make sure I grab it only once - from the first box in the chain.
For Each Sh In Doc.Shapes
With Sh.TextFrame
If .HasText And .Previous Is Nothing Then AllRanges.Add .ContainingRange
End With
Next Sh
End Function

Posted by Peter at September 19, 2006 02:08 PM

Comments

Post a comment




Remember Me?