Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

VBA - accessing all headers and footers

  • 08-06-2005 1:53pm
    #1
    Moderators, Arts Moderators Posts: 35,741 Mod ✭✭✭✭


    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?
    Sub CleanWholeDocument()
    
    Dim sShape As Shape
    Dim fNote As Footnote
    Dim sSection As Section
    Dim wWindow As Window
    Dim pPane As Pane
    Dim counter As Integer
    
    'Clean the headers and footers
    
    For Each wWindow In ActiveDocument.Windows
        For Each pPane In wWindow.Panes
            pPane.View.Type = wdPrintView
            pPane.View.SeekView = wdSeekCurrentPageHeader
            Selection.WholeStory
            RemoveTags
            pPane.View.SeekView = wdSeekCurrentPageFooter
            Selection.WholeStory
            RemoveTags
            pPane.View.SeekView = wdSeekMainDocument
        Next pPane
    Next wWindow
    
    'Clean the footnotes
    For Each fNote In ActiveDocument.Footnotes
        fNote.Range.Select
        RemoveTags
    Next fNote
    
    'Clean the textboxes and callouts
    For Each sShape In ActiveDocument.Shapes
        If sShape.TextFrame.HasText Then
            sShape.TextFrame.TextRange.Select
            RemoveTags
        End If
    Next sShape
    
    
    End Sub
    

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


Comments

  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    I have this done in a template at home. I'll post it later if I can find it. I could be wrong but off the top of my head I think you have to jump into the header/footer, then out if it into the maindocument for every page of the main document. I don't thing theres a master document object that includes headers and footers in the collection.

    I had to update all the field codes in a document in the correct order, the order being determined by their field name and they were spread through the headers/footers and in the main body. Word Basic saw all fields as the same range but VBA doesn't. You'll see what I mean if I find that example later.


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    Well this is what I have... I've just grabed the code I thought might be useful

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Selection.WholeStory                       'Select everything
    Selection.Fields.Update                     'Update all fields
    UpdateHeaders                               'Update all the headers
    
    
    Sub UpdateWholeStoryFields()
    
    Application.ScreenUpdating = False
    ActiveWindow.View.ShowFieldCodes = True     'Toggle field codes "on"
    Selection.WholeStory
    Selection.Fields.Update                     'Update all fields
    ActiveWindow.View.ShowFieldCodes = False     'Toggle field codes "Off"
    Selection.Collapse wdCollapseStart
    Application.ScreenUpdating = True
    
    End Sub
    
    Sub UpdateHeaders()
    ' Description: Loops through all the headers 
    ' in a document and updates the fields
    
    Dim oField As Field
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter
    For Each oSection In ActiveDocument.Sections
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
                For Each oField In oHeader.Range.Fields
                    oField.Update
                Next oField
            End If
        Next oHeader
    Next oSection
    
    End Sub
    
    Sub ooh
    
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader     'View the header
    ActiveWindow.View.ShowFieldCodes = True    'Toggle field codes "on"
    Selection.NextField                                     'Get the next field code
    Selection.WholeStory                                  'Select everything
    Selection.Fields.Update                                'Update all fields
    UpdateHeaders                                           'Update all the headers
    ActiveWindow.View.ShowFieldCodes = False     'Toggle field codes "off"
    Application.ScreenUpdating = True                  'Screen updating "on/off"
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument          'turn off header/footer (Mrk)
    
    End sub
    
    
    


Advertisement