Consulting

Results 1 to 5 of 5

Thread: Different header error when less than 2 pages

  1. #1

    Different header error when less than 2 pages

    Morning Folks,
    Nice looking forums lurked around a bit but couldn't find my solution. I am currently creating a macro to drop in the header and footer from 3 files ( header file, footer1, and footer2 ) just because it makes it easier to edit the text on the server once.

    The problem: if there is only one page then Active window does not see "wdSeekPrimaryFooter" returns an error and stops the macro. I need the macro to either possibly activate the field so that when a 2nd page does become present the 2nd footer is properly displayed or some other means I know there is a million different ways to do things with code. Any help with this or any tips on how this is written or could be improved I greatly appreciate trying to get a hang of this. -dro^
    Error code:
    [VBA]
    '2nd page footer "CurrentPageFooter"
    ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False
    [/VBA]



    Full code:

    [VBA]

    Sub LHMacroTemp()



    'Sets multiheader enviroment

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    With ActiveDocument.Sections(1)
    .PageSetup.DifferentFirstPageHeaderFooter = True

    End With








    '2nd page footer "CurrentPageFooter"
    ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False


    'Set FirstPageFooter
    ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageFooter
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertFile FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

    'Set FirstPageHeader
    ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader
    Selection.Collapse Direction:=wdCollapseEnd
    Selection.InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False

    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
    End If



    End Sub
    [/VBA]

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just to explain...

    Your code - if it is a one page document - makes that first page DifferentFirstPage. There is NO second page, therefore:[vba] '2nd page footer "CurrentPageFooter"
    ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter[/vba]does indeed error out. Part of the issue is that you are using SeekView.


    It is farfar better to action ANY action with headers and footer using ranges, not Selection. However, the code is trying to view Primary...and there is no Primary there to view. Thus, an error.

    So, either add another page so there IS a secondf page, OR action the Primary header directly. You can do that with code even though the page does not exist yet.
    [vba]Sub LHMacroTemp()
    With ActiveDocument.Sections(1)
    .PageSetup.DifferentFirstPageHeaderFooter = True
    End With
    '2nd page footer "CurrentPageFooter"
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
    FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

    'Set FirstPageFooter
    ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
    FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

    'Set FirstPageHeader
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
    ' center it
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    ' insert file
    .InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
    End With
    End Sub[/vba]This sets the Primary header and footer even though the page does not exist yet. The moment a second page is made, the header and footer will be there automatically.
    Last edited by fumei; 07-24-2012 at 05:30 PM.

  3. #3
    Quote Originally Posted by fumei
    Just to explain...

    Your code - if it is a one page document - makes that first page DifferentFirstPage. There is NO second page, therefore:[vba] '2nd page footer "CurrentPageFooter"
    ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter[/vba]does indeed error out. Part of the issue is that you are using SeekView.


    It is farfar better to action ANY action with headers and footer using ranges, not Selection. However, the code is trying to view Primary...and there is no Primary there to view. Thus, an error.

    So, either add another page so there IS a second page, OR action the Primary header directly. You can do that with code even though the page does not exist yet.
    I am sorry I am noob still, can you please show me an example of code to action the headers directly. Learning curve is steep with VB. Thank you for your help and response they kinda told me hey figure this out and left me in the wind at work. -_-

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]Sub LHMacroTemp()
    With ActiveDocument.Sections(1)
    .PageSetup.DifferentFirstPageHeaderFooter = True
    End With
    '2nd page footer "CurrentPageFooter"
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
    FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

    'Set FirstPageFooter
    ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
    FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

    'Set FirstPageHeader
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
    ' center it
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    ' insert file
    .InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
    End With
    End Sub[/vba]Even though there is no second page yet, its header and footer are set. When a second page is made, the header and footer will be there automatically.


    BTW, I find it a little odd that you ae inserting a .DOTM file into a header.

  5. #5

    Thumbs up

    Quote Originally Posted by fumei
    [vba]Sub LHMacroTemp()
    With ActiveDocument.Sections(1)
    .PageSetup.DifferentFirstPageHeaderFooter = True
    End With
    '2nd page footer "CurrentPageFooter"
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertFile _
    FileName:="N:\cpwin\macro\vbfoot2.dotx", Link:=False

    'Set FirstPageFooter
    ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range.InsertFil e _
    FileName:="N:\cpwin\macro\vbfoot1.docx", Link:=False

    'Set FirstPageHeader
    With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage).Range
    ' center it
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    ' insert file
    .InsertFile FileName:="N:\cpwin\macro\vbhead.dotm", Link:=False
    End With
    End Sub[/vba]Even though there is no second page yet, its header and footer are set. When a second page is made, the header and footer will be there automatically.


    BTW, I find it a little odd that you are inserting a .DOTM file into a header.

    With a little swap of .footer and .header everything worked perfectly.

    Well honestly I had no idea what I was doing so I was trying to figure out how word exports its macros and was creating all sorts of files in the beginning. Now I have one dotm file with the code inside a module which will be pushed with a login script to the word startup folder. I mean I was going crazy trying to figure out the linking using macro recorder. I have a vb book I am reading now but by the time I finish reading that I think they would have pushed this project to the back burner. I really thank you for the help this is exactly what I was looking for. Recommend any tutorials?

Posting Permissions

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