Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Jun 2003
    Location
    Surrey, England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VB6 Word Update all Headers and Footers (VB6 OfficeXP Word)

    I have a need to change the font of some text in the Headers and Footers of a large document. The document was written about 2 years ago and has come through time with many changes and additions and grown to about 70 pages. As such, it has many structures and I now need to re-format some of the text in all of the Headers and Footers. However, I do not want to touch the First page (which is different) - only from page 2 onwards.

    So far (even with Word's VBA record function), I have been unable to write some VB6 code to do this. The Headers & Footers contain a lot of references to Bookmarks and I use this code to refresh the references after bookmarks values have been changed:

    CGWord.ActiveDocument.Fields.Update
    For Each sec In CGDoc.Sections
    sec.Headers(wdHeaderFooterPrimary).Range.Fields.Up date
    sec.Headers(wdHeaderFooterFirstPage).Range.Fields. Update
    sec.Footers(wdHeaderFooterPrimary).Range.Fields.Up date
    sec.Footers(wdHeaderFooterFirstPage).Range.Fields. Update
    Next

    I cannot see how I can utilise this same piece of code to update the font of characters in all headers and footers. I am beginning to believe I might have to resort to using something like StoryRanges, but whilst I can code up something to update text in various Story ranges, I cannot see how to do this with Headers and Footers. The closest idea I have to update the Headers is shown below, but it has an illegal use of the StoryType Property:

    Dim Story As Variant
    Dim rngNext As Word.Range
    ' Iterate through all story types
    For Each Story In CGWord.ActiveDocument.StoryRanges
    ' Only Update fields in a header
    If Story.StoryType <> wdFirstPageHeaderStory Then
    CGWord.Selection.StoryType
    CGWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    CGWord.Selection.MoveDown Unit:=wdLine, Count:=1
    CGWord.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    CGWord.Selection.Font.Name = "Arial"
    CGWord.Selection.Font.Size = 10
    ' There may be linked headers so Update them as well
    Set rngNext = Story.NextStoryRange
    Do Until rngNext Is Nothing
    ' Update fields in this header
    CGWord.Selection.StoryType
    CGWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    CGWord.Selection.MoveDown Unit:=wdLine, Count:=1
    CGWord.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    CGWord.Selection.Font.Name = "Arial"
    CGWord.Selection.Font.Size = 10
    ' Link to next story (if any)
    Set rngNext = rngNext.NextStoryRange
    Loop
    End If
    Next

    I would appreciate any thoughts or comments on the suitablility of the above code, what might need to be changed or other ideas / suggestions to achieve the same effect.

    Here's hoping....

    Ken.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VB6 Word Update all Headers and Footers (VB6 OfficeXP Word)

    Sorry, only a few seconds at the moment, but some thoughts:

    First, you have to go section by section, so forget about story ranges and use your original loop.

    Second, almost anything you can do to a selection, you can do to a range. You know how to get the ranges for the footnotes, so try your selection-based code on those and see what happens. In some cases, you might need to back off the end of a range:

    dim rngtemp as range
    set rngtemp = sec.Headers(wdHeaderFooterPrimary).Range
    rngtemp.moveend wdcharacter, -1
    rngtemp.font....... etc.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •