locked
wdSeekCurrentPageHeader goes to previous page (word 2011) RRS feed

  • Question

  • I tried posting this in the Word for Mac forums but it won't let me submit. Since it is a VBA question, I'm hoping someone here will be able to help.

    I have some code that modifies headers and footers in new sections (found and modified from some helpful person on the internet). It works perfectly on my PC, but my code needs to work on the Mac and I'm having an odd problem. The code uses SeekView to get to the headers and footers, but wdSeekCurrentPageHeader doesn't go to the current page, it goes to the previous page. I've tried using LargeScroll to make sure that the only page in the view is the current one, and I've confirmed that the selection is correctly placed on the current page. Does anyone have any insight into this strange behavior? The problem is that I'm using a next page section break, but because CurrentPageHeader goes back a page, it's modifying my first section header, not my second section header. Here's a code snippet:

        ActiveWindow.ActivePane.View.Type = wdPageView
        For i = 1 To 2
            ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
            With Selection
            
                'Add some text to the header...
                .Expand wdStory
                .Delete
                .Paragraphs(1).TabStops.ClearAll
                .Style = ActiveDocument.Styles(styHead)
                .TypeText Text:="-"
                .Fields.Add Range:=.Range, Type:=wdFieldPage, Text:="\* Arabic"
                .Expand wdParagraph
                .Collapse wdCollapseEnd
                 .TypeText Text:="-"
               
                'Move focus to the Footer pane...
                ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
                
                .Expand wdStory
                .Delete
                .Style = ActiveDocument.Styles(wdStyleNormal)
        
            End With
            ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
            Selection.InsertBreak wdPageBreak

        Next i

    It's meant to add even and odd page header/footers for the current section (I then delete the extra page). But instead it's wiping out my previous section headers and footers. (The odd and even are identical for section 2, but not for section 1, so I've found that I need to add them as if they were different or my first page even headers pop in.)

    While the problem is happening on Word 2011, the solution also needs to be Word 2004 compatible, if that matters.

    Thanks!

    Edit: A little more information: It seems like CurrentPageHeader insists on going to an odd page header, so if I run my macro starting on an even-numbered page (so that the new section starts on an odd), it works. This information may lead me in the direction of a cumbersome workaround, should it prove to be consistently true.

    • Edited by cchimi Friday, September 13, 2013 4:46 PM more info
    Friday, September 13, 2013 4:15 PM

All replies

  • I have no access to Mac Word so I am unfamiliar with the limitations of its VBA, but if this was a PC, I would be inclined to work with ranges, which precludes the requirement to open the header or switch views. Maybe something like

    Dim oRng As Range
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter
    Dim oSection As Section

        Set oSection = Selection.Sections(1)
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
                oHeader.LinkToPrevious = False
                Set oRng = oHeader.Range
                With oRng
                    .Start = .End
                    .Style = ActiveDocument.Styles(styHead)
                    .Text = "-"
                    .Collapse wdCollapseEnd
                    .Fields.Add Range:=oRng, _
                                Type:=wdFieldPage, _
                                Text:="\* Arabic", _
                                PreserveFormatting:=False
                End With
            End If
        Next oHeader
        For Each oFooter In oSection.Footers
            If oFooter.Exists Then
                oFooter.LinkToPrevious = False
                Set oRng = oFooter.Range
                With oRng
                    'do stuff
                End With
            End If
        Next oFooter

    would work?


    Graham Mayor - Word MVP
    www.gmayor.com

    Saturday, September 14, 2013 2:35 PM
  • Thanks for the reply, and generally I would agree with you. But I've found that ranges in header/footers often don't work as desired; it becomes difficult to insert more than one piece of text (this is something I haven't worked on in a while, so I don't recall the specific issue, but it may be related to the many platforms I have to support).

    In any case, it seems like CurrentPageHeader on the Mac does not mean what it means on the PC; it takes you to the OddPageHeader and when there isn't one it jumps back to the last odd page. A bug, I assume. 

    Thursday, September 19, 2013 2:24 PM