none
Read page number of word document by interop word RRS feed

  • Question

  • Hi everyone!

    I have a c# application and I want to read page number field of each page.

    i.e. A,B,C... or 1,2,3... or i,ii,iii...

    Please Help!

    Thanks!

    Saturday, June 21, 2014 3:30 PM

Answers

  • Page # formats in Word are controlled at the Section level, though they can differ between first pages and odd/even pages. So, once you know the format for the first, odd & even pages in each Section, you have the formats for all pages in that section. Furthermore, since Section headers & footers can be linked to those of the preceding Section, you only need to get the data for each unlinked Section header/footer. Thus, with VBA, you can obtain comprehensive results via:

    Sub Demo1()
    Application.ScreenUpdating = False
    Dim Fld As Field, Sctn As Section, HdFt As HeaderFooter, StrRslt As String
    For Each Sctn In ActiveDocument.Sections
      For Each HdFt In Sctn.Headers
        If HdFt.LinkToPrevious = False Then
          For Each Fld In HdFt.Range.Fields
            If Fld.Type = wdFieldPage Then
              StrRslt = StrRslt & "Section " & Sctn.Index & _
                " Header " & HdFt.Index & _
                " Page # format " & HdFt.PageNumbers.NumberStyle & _
                " & starting #: " & Fld.Result & vbCr
            End If
          Next
        End If
      Next
      For Each HdFt In Sctn.Footers
        If HdFt.LinkToPrevious = False Then
          For Each Fld In HdFt.Range.Fields
            If Fld.Type = wdFieldPage Then
              StrRslt = StrRslt & "Section " & Sctn.Index & _
                " Footer " & HdFt.Index & _
                " Page # format " & HdFt.PageNumbers.NumberStyle & _
                " & starting #: " & Fld.Result & vbCr
            End If
          Next
        End If
      Next
    Next
    MsgBox StrRslt
    End Sub

    I'll leave it to you to do the C# conversion.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, June 22, 2014 4:19 AM

All replies

  • Hi Mirosi

    I'm not sure I understand your question: What do you mean by "page number field"? Please provide more details, including the LOCATION of the field in relation to the document / page.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 21, 2014 6:14 PM
    Moderator
  • Page # formats in Word are controlled at the Section level, though they can differ between first pages and odd/even pages. So, once you know the format for the first, odd & even pages in each Section, you have the formats for all pages in that section. Furthermore, since Section headers & footers can be linked to those of the preceding Section, you only need to get the data for each unlinked Section header/footer. Thus, with VBA, you can obtain comprehensive results via:

    Sub Demo1()
    Application.ScreenUpdating = False
    Dim Fld As Field, Sctn As Section, HdFt As HeaderFooter, StrRslt As String
    For Each Sctn In ActiveDocument.Sections
      For Each HdFt In Sctn.Headers
        If HdFt.LinkToPrevious = False Then
          For Each Fld In HdFt.Range.Fields
            If Fld.Type = wdFieldPage Then
              StrRslt = StrRslt & "Section " & Sctn.Index & _
                " Header " & HdFt.Index & _
                " Page # format " & HdFt.PageNumbers.NumberStyle & _
                " & starting #: " & Fld.Result & vbCr
            End If
          Next
        End If
      Next
      For Each HdFt In Sctn.Footers
        If HdFt.LinkToPrevious = False Then
          For Each Fld In HdFt.Range.Fields
            If Fld.Type = wdFieldPage Then
              StrRslt = StrRslt & "Section " & Sctn.Index & _
                " Footer " & HdFt.Index & _
                " Page # format " & HdFt.PageNumbers.NumberStyle & _
                " & starting #: " & Fld.Result & vbCr
            End If
          Next
        End If
      Next
    Next
    MsgBox StrRslt
    End Sub

    I'll leave it to you to do the C# conversion.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, June 22, 2014 4:19 AM
  • Hi Cindy

    If your word document has page numbers then those are on the header or footer of the page.

    same the way you can insert page numbers in a word document.

    If page number is a field or anything I'd like to read value of them for every page.

    Thanks!

    Saturday, June 28, 2014 7:34 AM
  • Hi Mirosi

    The problem is that, when a PAGE field is in the header or footer you can't specifically extract the value being displayed on any single page. When you access a Header or Footer range, you're accessing it through a Section property. You can get the content of the Header or Footer for that section, but not for any particular page in the section.

    One reason for this is that, as far as Word is concerned, pages don't really exist as objects. They're generated on-the-fly, depending on the printer driver and other settings.

    That's why I asked you the way I did - it you'd inserted the page numbers into the body text of the document, then you could pick up each individual field, in the manner you ask. This simply is not possible when the fields are in the Header/Footer. You can calculate what the document should be displaying - see Paul's reply that Luna as marked as "Answer" and see if the gives you the result you want?


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 28, 2014 11:38 AM
    Moderator
  • Thanks by Paul's Answer I found my solution.

    Sub GetPageNumbers()
    Dim StrRslt As String, Fld As Field, HdFt As HeaderFooter, rng As Range, strPageNumber As String
    Application.ScreenUpdating = False
    ' Goto footer and finding page number
    For i = 1 To Selection.Information(wdNumberOfPagesInDocument)
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i
        strPageNumber = ""
        With ActiveDocument.ActiveWindow.View
            .Type = wdPrintView
            .SeekView = wdSeekCurrentPageFooter
        End With
        
        For Each Fld In Selection.HeaderFooter.Range.Fields
            If Fld.Type = wdFieldPage Then
                   strPageNumber = strPageNumber & "Footer Page Num: " & Fld.Result & vbCrLf
            End If
        Next
        
        If strPageNumber = "" Then
            ' Goto header and finding page number
            With ActiveDocument.ActiveWindow.View
                .Type = wdPrintView
                .SeekView = wdSeekCurrentPageHeader
            End With
            For Each Fld In Selection.HeaderFooter.Range.Fields
                If Fld.Type = wdFieldPage Then
                       strPageNumber = strPageNumber & "Header Page Num: " & Fld.Result & vbCrLf
                End If
            Next
        End If
        
        If strPageNumber = "" Then
            ' Setting page number by a default value
            strPageNumber = "Default Page Num: " & i & vbCrLf
        End If
        
        With ActiveDocument.ActiveWindow.View
            .Type = wdPrintView
            .SeekView = wdSeekMainDocument
        End With
    
        StrRslt = StrRslt & strPageNumber
    Next
    
    MsgBox StrRslt
    End Sub

    I hope to be helpful.

    Saturday, June 28, 2014 12:25 PM