none
How to keep the page number in the MS Word footer while updating footer information using macro/VB.net? RRS feed

  • Question

  • I need to update footer and header programatically using VB.net. I found overwrite header and footer is much efficient than just update section of the header/footer.  Everything seems working fine but when user entered page number in header/footer section, it will be deleted when I overwrite header and footer. Is there anyway to check page number exist in the header and footer using VB.net?

    Any help will be greatly appreciated.

    Friday, August 10, 2012 1:07 PM

Answers

  • Here's some vba code (easily enough adapted to vb.net) showing how you can define the range that has the page numbers in a document's primary header. The code caters for a simple page # all the way through to a 'Page X of Y' construction.

    Sub Test()
    Dim Fld As Field, Rng As Range
    Const wdFieldNumPages As Long = 26
    Const wdFieldPage As Long = 33
    Const wdFieldSectionPages As Long = 66
    Const wdWord As Long = 2
    Const wdHeaderFooterPrimary = 1
    Const wdHeaderFooterFirstPage = 2
    Const wdHeaderFooterEvenPages = 3
    With ActiveDocument.Sections.First.Headers(wdHeaderFooterPrimary).Range
      For Each Fld In .Fields
        With Fld
          If .Type = wdFieldPage Then Set Rng = .Result
          If .Type = wdFieldNumPages Or .Type = wdFieldSectionPages Then
            If Not Rng Is Nothing Then Rng.End = .Result.End
          End If
        End With
      Next
      If Not Rng Is Nothing Then
        If Rng.Words.First.Previous.Words.First = "Page " Then
          Rng.MoveStart wdWord, -1
        End If
        MsgBox Rng.Text
      End If
    End With
    End Sub

    Note that each Section in a Word document has three headers and footers, but I don't know which one(s) you're working with. Having identified the page numbering range, you can now modify the header/footer content before/after it.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Monday, August 13, 2012 10:42 PM

All replies

  • You can check whether the footer contains any fields and, if so, whether those fields are PAGE fields.

    Note that, if the user has 'Page X of Y' numbering, you will also need to check for NUMPAGES and SECTIONPAGES fields as well - and these might even be embedded in other fields. Furthermore, some scenarios use SET fields in un-numbered sections to adjust the 'Y' part. Accordingly, your whole approach may need to be re-thought.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 10, 2012 11:25 PM
  • Paul,

    Thanks for reply, I was trying to check the NUMPAGES and SECTIONPAGES fields but I could not find it.

    How do I check those fields programatically? where do I look? I was looking at ActiveDocument object

    Thanks in advance,

    Monday, August 13, 2012 8:05 PM
  • Here's some vba code (easily enough adapted to vb.net) showing how you can define the range that has the page numbers in a document's primary header. The code caters for a simple page # all the way through to a 'Page X of Y' construction.

    Sub Test()
    Dim Fld As Field, Rng As Range
    Const wdFieldNumPages As Long = 26
    Const wdFieldPage As Long = 33
    Const wdFieldSectionPages As Long = 66
    Const wdWord As Long = 2
    Const wdHeaderFooterPrimary = 1
    Const wdHeaderFooterFirstPage = 2
    Const wdHeaderFooterEvenPages = 3
    With ActiveDocument.Sections.First.Headers(wdHeaderFooterPrimary).Range
      For Each Fld In .Fields
        With Fld
          If .Type = wdFieldPage Then Set Rng = .Result
          If .Type = wdFieldNumPages Or .Type = wdFieldSectionPages Then
            If Not Rng Is Nothing Then Rng.End = .Result.End
          End If
        End With
      Next
      If Not Rng Is Nothing Then
        If Rng.Words.First.Previous.Words.First = "Page " Then
          Rng.MoveStart wdWord, -1
        End If
        MsgBox Rng.Text
      End If
    End With
    End Sub

    Note that each Section in a Word document has three headers and footers, but I don't know which one(s) you're working with. Having identified the page numbering range, you can now modify the header/footer content before/after it.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Monday, August 13, 2012 10:42 PM
  • Hi,

    I am also trying to use this code to check whether the word document has page numbers are not... but it is not giving any message or prompt ...

    I need a macro to check in a document whether the page number has been added manualy or using the format page number -> Page starts at ---

    in the footer of the document

    Can there be a prompt of message if the page number has been entered manualy in the footer ..

    Thanks in advance


    • Edited by ALI74 Tuesday, January 1, 2013 8:30 AM
    Tuesday, January 1, 2013 8:22 AM
  • I am also trying to use this code to check whether the word document has page numbers are not... but it is not giving any message or prompt ...

    I need a macro to check in a document whether the page number has been added manualy or using the format page number -> Page starts at ---

    in the footer of the document


    That is not what the code discussed in this thread is for. The purpose is as described in the initial post.

    Furthermore, you cannot tell whether the page number field in a given Section (other than the first), has been "added manualy or using the format page number -> Page starts at", unless the Section in question uses a different page numbering scheme than the one preceding it. And you don't need code for that.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, January 1, 2013 10:20 AM
  • Hi Paul,

    Thanks for the reply

    I will elobrate my requirement again in a detailed way in a new thread


    ALI

    Tuesday, January 1, 2013 12:18 PM