Results 1 to 6 of 6

Thread: [RESOLVED] How to count number of pages in a document?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Resolved [RESOLVED] How to count number of pages in a document?

    Hi
    I want to know how many pages my word document has. Normally, i thought I should get it as a property of Activedocument. , but it isnt the case.
    Anyone got an idea how to do it?
    thanks a lot

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Thumbs up Re: How to count number of pages in a document?

    check this code

    VB Code:
    1. Dim SR As New StreamReader(FileName)
    2.             Dim PDFData As String = SR.ReadToEnd
    3.  
    4.                      ''Slow but Simple method : Good for small files
    5.             ''
    6.             'Dim c As Int16
    7.             'While PDFData.IndexOf("/Type /Page") <> -1
    8.             '    'Should be "/Page" and not "/Pages". So check for "s"  
    9.             '    If PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11, 1) <> "s" Then
    10.             '        c += 1
    11.             '    End If
    12.             '    PDFData = PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11)
    13.             'End While
    14.             'PageCount = c
    15.                        ''Faster but a bit lengthy : Good for large files
    16.             ''
    17.             ''Temp Variables
    18.             Dim TypePagesIndex As Integer
    19.             Dim StartIndex As Integer   'Starting index of the Pages Object
    20.             Dim EndIndex As Integer     'Ending index of the Pages Object
    21.             Dim CountIndex As Int16     'Starting index of "/Count"
    22.             Dim chars() As Char = {"/", ">"}
    23.             Dim tmp As String
    24.             Dim tmpIndex1, tmpIndex2 As Integer
    25.             Dim CountEndIndex As Int16  'Index of next "/" after "/Count"
    26.            
    27.             Do
    28.                 'Get an Object of type 'Pages' from PDF file
    29.                 'It can be "/Type /Pages" or "/Type/Pages"
    30.                 tmpIndex1 = PDFData.IndexOf("/Type /Pages")
    31.                 tmpIndex2 = PDFData.IndexOf("/Type/Pages")
    32.                 'Different possibilities of 2 indices
    33.                 If tmpIndex1 > -1 And tmpIndex1 < tmpIndex2 Then
    34.                     TypePagesIndex = tmpIndex1
    35.                 ElseIf tmpIndex2 > -1 And tmpIndex2 < tmpIndex1 Then
    36.                     TypePagesIndex = tmpIndex2
    37.                 ElseIf tmpIndex1 = -1 And tmpIndex2 > -1 Then
    38.                     TypePagesIndex = tmpIndex2
    39.                 ElseIf tmpIndex2 = -1 And tmpIndex1 > -1 Then
    40.                     TypePagesIndex = tmpIndex1
    41.                 Else  'tmpIndex1 = -1 And tmpIndex2 = -1
    42.                     Exit Do
    43.                 End If
    44.                 tmp = PDFData.Substring(0, TypePagesIndex)
    45.                 StartIndex = tmp.LastIndexOf("<<")
    46.                 tmp = PDFData.Substring(TypePagesIndex)
    47.                 EndIndex = TypePagesIndex + tmp.IndexOf(">>") + 1
    48.                 tmp = PDFData.Substring(StartIndex, EndIndex - StartIndex + 1)
    49.                 'Now tmp="<< /Kids, /Count etc >>"
    50.                 'the pagecount is just after "/Count " in tmp
    51.                 CountIndex = tmp.IndexOf("/Count")
    52.                 CountIndex += 7  'Move index to the end of "/Count "
    53.                 tmp = tmp.Substring(CountIndex)
    54.                 'now tmp="Pagecount ....>>"
    55.                 'Pagecount is followd by a newline like char and then "/" or ">>"
    56.                 CountEndIndex = tmp.IndexOfAny(chars)
    57.                 tmp = tmp.Substring(0, CountEndIndex) 'Get the PageCount
    58.  
    59.                 If PageCount < Val(tmp) Then PageCount = Val(tmp)
    60.                 PDFData = PDFData.Substring(EndIndex + 1)
    61.             Loop
    62.           MsgBox("# Pages = " & PageCount)

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: How to count number of pages in a document?

    Even though that code will work, its .NET. What are you needing this solution in?

    Check out my Word FAQ on reading a documents Built-in properties.
    http://vbforums.com/showthread.php?t=409760

    Just use the wdPropertyPages constant to return a Page count of your document. Should only take one or two lines of code as seen in my code example.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How to count number of pages in a document?

    Thanx a lot guys for your responses.
    However, Shakti, your code is too long and I can't use it in a macro, but thanx buddy
    Rob, what to say...Rob you rock! it's exactly what I was searching for and you have always the right answer to the problems, keep it up, it's a great contribution

  5. #5
    New Member
    Join Date
    Jan 2018
    Posts
    1

    Re: How to count number of pages in a document?

    Quote Originally Posted by shakti5385 View Post
    check this code

    VB Code:
    1. Dim SR As New StreamReader(FileName)
    2.             Dim PDFData As String = SR.ReadToEnd
    3.  
    4.                      ''Slow but Simple method : Good for small files
    5.             ''
    6.             'Dim c As Int16
    7.             'While PDFData.IndexOf("/Type /Page") <> -1
    8.             '    'Should be "/Page" and not "/Pages". So check for "s"  
    9.             '    If PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11, 1) <> "s" Then
    10.             '        c += 1
    11.             '    End If
    12.             '    PDFData = PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11)
    13.             'End While
    14.             'PageCount = c
    15.                        ''Faster but a bit lengthy : Good for large files
    16.             ''
    17.             ''Temp Variables
    18.             Dim TypePagesIndex As Integer
    19.             Dim StartIndex As Integer   'Starting index of the Pages Object
    20.             Dim EndIndex As Integer     'Ending index of the Pages Object
    21.             Dim CountIndex As Int16     'Starting index of "/Count"
    22.             Dim chars() As Char = {"/", ">"}
    23.             Dim tmp As String
    24.             Dim tmpIndex1, tmpIndex2 As Integer
    25.             Dim CountEndIndex As Int16  'Index of next "/" after "/Count"
    26.            
    27.             Do
    28.                 'Get an Object of type 'Pages' from PDF file
    29.                 'It can be "/Type /Pages" or "/Type/Pages"
    30.                 tmpIndex1 = PDFData.IndexOf("/Type /Pages")
    31.                 tmpIndex2 = PDFData.IndexOf("/Type/Pages")
    32.                 'Different possibilities of 2 indices
    33.                 If tmpIndex1 > -1 And tmpIndex1 < tmpIndex2 Then
    34.                     TypePagesIndex = tmpIndex1
    35.                 ElseIf tmpIndex2 > -1 And tmpIndex2 < tmpIndex1 Then
    36.                     TypePagesIndex = tmpIndex2
    37.                 ElseIf tmpIndex1 = -1 And tmpIndex2 > -1 Then
    38.                     TypePagesIndex = tmpIndex2
    39.                 ElseIf tmpIndex2 = -1 And tmpIndex1 > -1 Then
    40.                     TypePagesIndex = tmpIndex1
    41.                 Else  'tmpIndex1 = -1 And tmpIndex2 = -1
    42.                     Exit Do
    43.                 End If
    44.                 tmp = PDFData.Substring(0, TypePagesIndex)
    45.                 StartIndex = tmp.LastIndexOf("<<")
    46.                 tmp = PDFData.Substring(TypePagesIndex)
    47.                 EndIndex = TypePagesIndex + tmp.IndexOf(">>") + 1
    48.                 tmp = PDFData.Substring(StartIndex, EndIndex - StartIndex + 1)
    49.                 'Now tmp="<< /Kids, /Count etc >>"
    50.                 'the pagecount is just after "/Count " in tmp
    51.                 CountIndex = tmp.IndexOf("/Count")
    52.                 CountIndex += 7  'Move index to the end of "/Count "
    53.                 tmp = tmp.Substring(CountIndex)
    54.                 'now tmp="Pagecount ....>>"
    55.                 'Pagecount is followd by a newline like char and then "/" or ">>"
    56.                 CountEndIndex = tmp.IndexOfAny(chars)
    57.                 tmp = tmp.Substring(0, CountEndIndex) 'Get the PageCount
    58.  
    59.                 If PageCount < Val(tmp) Then PageCount = Val(tmp)
    60.                 PDFData = PDFData.Substring(EndIndex + 1)
    61.             Loop
    62.           MsgBox("# Pages = " & PageCount)

    This code worked. If there any chance that I could also count .doc/ .docx page numbers?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] How to count number of pages in a document?

    as in post #3, for word documents, you can use built in properties

    Code:
        MsgBox ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width