none
Word 2010: VBA to set footers

    Question

  • Hello All,

    I am trying to have a footer in the following way:

    Footer in first page: 'Jane'

    Footer in second Page: 'Jane-1'

    Footer in third Page: 'Jane-2'

    and so on.. but I also have page numbers in the header (Page 1 in first page, Page 2 in second page etc).

    How can we get this using vba. We can get the footer part if we set .startingnumber=0 but this is making my header to show one page less (second page has the header Page-1 etc). How can we solve this issue?

    Monday, October 07, 2013 10:25 PM

Answers

  • Hi VD

    Actually, I think you're asking this question in the wrong place. I don't think programming code is going to be a solution, meaning the better place for a discussion would be in the end-user "Communities":

    http://answers.microsoft.com/en-us/office/forum/word

    FWIW, though, if I'm understanding your requirement correctly, I'd proceed as follows:

    1. Activate "Different First Page" for the document. That will let you set thing up differently for the first page header and footer ("The only exception is for first page where the footer should just show 'Jane' and NOT 'Jane-0'.")

    2. The second and following pages then contain page numbering across the board. Personally, I'd add 1 to the header and let the footer page numbering alone. Go to the header, press Alt+F9 to display the field codes. In the header you should see: { PAGE }.

    Select all of that, then press Ctrl+F9 to insert field codes around it. Between the two opening field braces, type: =

    Between the two closing field braces, type: + 1

    The resulting field code: { = { PAGE } + 1 }

    Press F9 to update the field code; Alt+F9 to toggle back to the field display in the document.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 4:01 PM
    Moderator
  • Try:

    Sub Demo()
    Dim Rng As Range
    Application.ScreenUpdating = False
    With ActiveDocument
      .ActiveWindow.View.ShowFieldCodes = True
      With .Sections.First
        With .Headers(wdHeaderFooterPrimary)
          With .Range
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="PAGE", Preserveformatting:=False
          End With
          .PageNumbers.StartingNumber = 1
        End With
        With .Footers(wdHeaderFooterPrimary)
          Set Rng = .Range
          With .Range
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="=1- \# ;'-'0;", Preserveformatting:=False
             .MoveStart wdCharacter, 5
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="PAGE", Preserveformatting:=False
          End With
          Rng.InsertBefore "Jane"
        End With
      End With
      .ActiveWindow.View.ShowFieldCodes = False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, October 12, 2013 2:07 AM

All replies

  • The best way of doing this depends on whether you need to cross-reference the page #s from anywhere within the body of the document (eg for a Table of Contents or a heading page # cross-reference).

    If you do need to cross-reference the page #s, insert a Next page Section break between the first two pages, then unlink the page headers before inserting 'Jane' on the first Section's page header and 'Jane-' plus the page # field (formatted to start at 1) on the second page header.

    Otherwise, you could forego the Section breaks etc and use a simple field code:
    Jane{Page \#'-'0;;}
    with the page numbering formatted to start at 0.

    Note: The field brace pairs (ie '{ }') for the above example are created in the body of the document via Ctrl-F9 (Cmd-F9 on a Mac) - you can't simply type them or copy & paste them from this message.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, October 07, 2013 11:59 PM
  • Hello Paul,

    I am not sure if I was clear in my previous post.

    First, I dont have any cross referencing. Now, I want to see 'Page 1' in first page, 'Page 2' in second page etc but the footer in first page should be 'Jane' and in second page it should be Jane-1, in third page it should be Jane-2 etc.

    I have a macro which generates word docs, so 'Jane' is constant only for one particular word doc and then for the other word doc it is 'Patrick' etc. I hope this helps :)

    Tuesday, October 08, 2013 11:22 AM
  • Try something based on:

    With ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary)
      With .Range
        .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
          Text:="PAGE \# '-'0;;", Preserveformatting:=False
        .InsertBefore "Jane"
      End With
      .PageNumbers.StartingNumber = 0
    End With


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Tuesday, October 08, 2013 8:24 PM
    Tuesday, October 08, 2013 8:23 PM
  • I appreciate your help. Thanks Paul. But it works partially. I have searched a lot and tested a lot and even tried to understand the code but not with much luck!

    Probably I dint communicate what I am trying to do. I request you to please go through it in detail:

    In each page of a word document, I have a header and a footer. Header in each page has page number in it.

    What the above code currently does:

    Header in the first page shows 'Page 0' (but I want it to show 'Page 1').

    Footer in first page shows nothing (but I was trying to have just 'Jane').

    Header in second page shows 'Page 1' (but I want it to show 'Page 2').

    Footer in second page shows 'Jane-1' (This is correct but I also want the header in the second page to show as 'Page 2' and not 'Page 1').

    Header in third page shows 'Page 2' (but I want it to show 'Page 3').

    Footer in third page shows 'Jane-2' (This is correct but I also want the header in the third page to show as 'Page 3' and NOT 'Page 2').

    In general: Header in nth page should show 'Page n' and footer in the same page should show 'Jane-(n-1)'.

    The only exception is for first page where the footer should just show 'Jane' and NOT 'Jane-0'.

    Is this possible at all? Please let me know.

    Friday, October 11, 2013 11:38 AM
  • Hi VD

    Actually, I think you're asking this question in the wrong place. I don't think programming code is going to be a solution, meaning the better place for a discussion would be in the end-user "Communities":

    http://answers.microsoft.com/en-us/office/forum/word

    FWIW, though, if I'm understanding your requirement correctly, I'd proceed as follows:

    1. Activate "Different First Page" for the document. That will let you set thing up differently for the first page header and footer ("The only exception is for first page where the footer should just show 'Jane' and NOT 'Jane-0'.")

    2. The second and following pages then contain page numbering across the board. Personally, I'd add 1 to the header and let the footer page numbering alone. Go to the header, press Alt+F9 to display the field codes. In the header you should see: { PAGE }.

    Select all of that, then press Ctrl+F9 to insert field codes around it. Between the two opening field braces, type: =

    Between the two closing field braces, type: + 1

    The resulting field code: { = { PAGE } + 1 }

    Press F9 to update the field code; Alt+F9 to toggle back to the field display in the document.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 4:01 PM
    Moderator
  • Dear Cindy,

    Thanks for the reply.

    Your solution looks like what I want.

    The only issue is I am not able to update the field code.

    I have pressed F9.

    I have also run sample macro which claimed that it would update the fields but it dint.

    I changed the view to print mode and then back but nothing changed. Please help.

    I also pressed Ctrl-A then F9 but again nothing changed. I just see the modified field code in the header now.
    • Edited by VD Chauhan Friday, October 11, 2013 6:23 PM It was not complete
    Friday, October 11, 2013 6:21 PM
  • Did you use Ctrl+F9 to create the field code brackets? You MUST use Ctrl+F9, typing { } from the keyboard will not work...

    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 6:34 PM
    Moderator
  • This is just awesome. It worked!

    I forgot to add a '=' sign in the field hence the issue.

    Thank you so much!

    Friday, October 11, 2013 6:45 PM
  • Try:

    Sub Demo()
    Dim Rng As Range
    Application.ScreenUpdating = False
    With ActiveDocument
      .ActiveWindow.View.ShowFieldCodes = True
      With .Sections.First
        With .Headers(wdHeaderFooterPrimary)
          With .Range
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="PAGE", Preserveformatting:=False
          End With
          .PageNumbers.StartingNumber = 1
        End With
        With .Footers(wdHeaderFooterPrimary)
          Set Rng = .Range
          With .Range
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="=1- \# ;'-'0;", Preserveformatting:=False
             .MoveStart wdCharacter, 5
            .Fields.Add Range:=.Characters.First, Type:=wdFieldEmpty, _
              Text:="PAGE", Preserveformatting:=False
          End With
          Rng.InsertBefore "Jane"
        End With
      End With
      .ActiveWindow.View.ShowFieldCodes = False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, October 12, 2013 2:07 AM