PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Word VB - VBA - accessing all headers and footers-VBForums
Results 1 to 4 of 4

Thread: Word VB - VBA - accessing all headers and footers

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Word VB - VBA - accessing all headers and footers

    I'm trying to write a VB macro for Word that goes through each header and footer in a document and performs an action (doesn't really matter what, it's the accessing headers/footers that's stumping me). I've been going around in circles with windows, panes, headers, footers, headerfooters, sections and still not got one solution that works for the whole document. This code works, but only for the first section in the document. Can anyone point me in the right direction?

    VB Code:
    1. Sub CleanWholeDocument()
    2.  
    3. Dim sShape As Shape
    4. Dim fNote As Footnote
    5. Dim sSection As Section
    6. Dim wWindow As Window
    7. Dim pPane As Pane
    8. Dim counter As Integer
    9.  
    10. 'Clean the headers and footers
    11.  
    12. For Each wWindow In ActiveDocument.Windows
    13.     For Each pPane In wWindow.Panes
    14.         pPane.View.Type = wdPrintView
    15.         pPane.View.SeekView = wdSeekCurrentPageHeader
    16.         Selection.WholeStory
    17.         RemoveTags
    18.         pPane.View.SeekView = wdSeekCurrentPageFooter
    19.         Selection.WholeStory
    20.         RemoveTags
    21.         pPane.View.SeekView = wdSeekMainDocument
    22.     Next pPane
    23. Next wWindow
    24.  
    25. 'Clean the footnotes
    26. For Each fNote In ActiveDocument.Footnotes
    27.     fNote.Range.Select
    28.     RemoveTags
    29. Next fNote
    30.  
    31. 'Clean the textboxes and callouts
    32. For Each sShape In ActiveDocument.Shapes
    33.     If sShape.TextFrame.HasText Then
    34.         sShape.TextFrame.TextRange.Select
    35.         RemoveTags
    36.     End If
    37. Next sShape
    38.  
    39.  
    40. End Sub

    Only the first For Loop is relevant to the question, but I left it all in for the sake of completeness.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,726

    Re: Word VB - VBA - accessing all headers and footers

    from the help file

    Headers Property Example

    This example adds centered page numbers to every page in the active document except the first. (A separate header is created for the first page.)

    VB Code:
    1. With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
    2.     .PageNumbers.Add _
    3.         PageNumberAlignment:=wdAlignPageNumberCenter, _
    4.         FirstPage:=False
    5. End With
    This example adds text to the first-page header in the active document.

    ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    VB Code:
    1. With ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
    2.     .Range.InsertAfter("First Page Text")
    3.     .Range.Paragraphs.Alignment = wdAlignParagraphRight
    4. End With

    maybe this can help you

    you can loop through the headerfooters collection, but may have to do it for each section of the document
    pete

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Word VB - VBA - accessing all headers and footers

    The problem I keep meeting with accessing the HeaderFooters is that the header/footer/footnote window opens in the bottom and stays stuck.

    VB Code:
    1. For Each sSection In ActiveDocument.Sections
    2.     sSection.Headers(wdHeaderFooterPrimary).Range.Select
    3.     RemoveTags
    4. Next sSection

    This was the way I tried to approach it first, but then switched to the Windows/Panes idea, which works properly but only on the first section. To be perfectly honest, I'm not sure what the Windows and Panes physically correspond to, but I think some combination of the two approaches (windows and sections/headers/footers) should be the solution.
    Attached Images Attached Images  
    Last edited by pickarooney; Jun 9th, 2005 at 06:07 AM.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,726

    Re: Word VB - VBA - accessing all headers and footers

    i think you would also have to loop throught the headers and footers collection within each section, as there is at least 3 types of headers and footers

    then to stop the last footer being stuck open, select somewhere else in the document

    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width