-
Oct 14th, 2006, 10:37 AM
#1
Thread Starter
Member
[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
-
Oct 14th, 2006, 11:18 AM
#2
Re: How to count number of pages in a document?
check this code
VB Code:
Dim SR As New StreamReader(FileName)
Dim PDFData As String = SR.ReadToEnd
''Slow but Simple method : Good for small files
''
'Dim c As Int16
'While PDFData.IndexOf("/Type /Page") <> -1
' 'Should be "/Page" and not "/Pages". So check for "s"
' If PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11, 1) <> "s" Then
' c += 1
' End If
' PDFData = PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11)
'End While
'PageCount = c
''Faster but a bit lengthy : Good for large files
''
''Temp Variables
Dim TypePagesIndex As Integer
Dim StartIndex As Integer 'Starting index of the Pages Object
Dim EndIndex As Integer 'Ending index of the Pages Object
Dim CountIndex As Int16 'Starting index of "/Count"
Dim chars() As Char = {"/", ">"}
Dim tmp As String
Dim tmpIndex1, tmpIndex2 As Integer
Dim CountEndIndex As Int16 'Index of next "/" after "/Count"
Do
'Get an Object of type 'Pages' from PDF file
'It can be "/Type /Pages" or "/Type/Pages"
tmpIndex1 = PDFData.IndexOf("/Type /Pages")
tmpIndex2 = PDFData.IndexOf("/Type/Pages")
'Different possibilities of 2 indices
If tmpIndex1 > -1 And tmpIndex1 < tmpIndex2 Then
TypePagesIndex = tmpIndex1
ElseIf tmpIndex2 > -1 And tmpIndex2 < tmpIndex1 Then
TypePagesIndex = tmpIndex2
ElseIf tmpIndex1 = -1 And tmpIndex2 > -1 Then
TypePagesIndex = tmpIndex2
ElseIf tmpIndex2 = -1 And tmpIndex1 > -1 Then
TypePagesIndex = tmpIndex1
Else 'tmpIndex1 = -1 And tmpIndex2 = -1
Exit Do
End If
tmp = PDFData.Substring(0, TypePagesIndex)
StartIndex = tmp.LastIndexOf("<<")
tmp = PDFData.Substring(TypePagesIndex)
EndIndex = TypePagesIndex + tmp.IndexOf(">>") + 1
tmp = PDFData.Substring(StartIndex, EndIndex - StartIndex + 1)
'Now tmp="<< /Kids, /Count etc >>"
'the pagecount is just after "/Count " in tmp
CountIndex = tmp.IndexOf("/Count")
CountIndex += 7 'Move index to the end of "/Count "
tmp = tmp.Substring(CountIndex)
'now tmp="Pagecount ....>>"
'Pagecount is followd by a newline like char and then "/" or ">>"
CountEndIndex = tmp.IndexOfAny(chars)
tmp = tmp.Substring(0, CountEndIndex) 'Get the PageCount
If PageCount < Val(tmp) Then PageCount = Val(tmp)
PDFData = PDFData.Substring(EndIndex + 1)
Loop
MsgBox("# Pages = " & PageCount)
-
Oct 14th, 2006, 02:34 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Oct 14th, 2006, 04:41 PM
#4
Thread Starter
Member
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
-
Jan 28th, 2018, 02:15 PM
#5
New Member
Re: How to count number of pages in a document?
Originally Posted by shakti5385
check this code
VB Code:
Dim SR As New StreamReader(FileName)
Dim PDFData As String = SR.ReadToEnd
''Slow but Simple method : Good for small files
''
'Dim c As Int16
'While PDFData.IndexOf("/Type /Page") <> -1
' 'Should be "/Page" and not "/Pages". So check for "s"
' If PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11, 1) <> "s" Then
' c += 1
' End If
' PDFData = PDFData.Substring(PDFData.IndexOf("/Type /Page") + 11)
'End While
'PageCount = c
''Faster but a bit lengthy : Good for large files
''
''Temp Variables
Dim TypePagesIndex As Integer
Dim StartIndex As Integer 'Starting index of the Pages Object
Dim EndIndex As Integer 'Ending index of the Pages Object
Dim CountIndex As Int16 'Starting index of "/Count"
Dim chars() As Char = {"/", ">"}
Dim tmp As String
Dim tmpIndex1, tmpIndex2 As Integer
Dim CountEndIndex As Int16 'Index of next "/" after "/Count"
Do
'Get an Object of type 'Pages' from PDF file
'It can be "/Type /Pages" or "/Type/Pages"
tmpIndex1 = PDFData.IndexOf("/Type /Pages")
tmpIndex2 = PDFData.IndexOf("/Type/Pages")
'Different possibilities of 2 indices
If tmpIndex1 > -1 And tmpIndex1 < tmpIndex2 Then
TypePagesIndex = tmpIndex1
ElseIf tmpIndex2 > -1 And tmpIndex2 < tmpIndex1 Then
TypePagesIndex = tmpIndex2
ElseIf tmpIndex1 = -1 And tmpIndex2 > -1 Then
TypePagesIndex = tmpIndex2
ElseIf tmpIndex2 = -1 And tmpIndex1 > -1 Then
TypePagesIndex = tmpIndex1
Else 'tmpIndex1 = -1 And tmpIndex2 = -1
Exit Do
End If
tmp = PDFData.Substring(0, TypePagesIndex)
StartIndex = tmp.LastIndexOf("<<")
tmp = PDFData.Substring(TypePagesIndex)
EndIndex = TypePagesIndex + tmp.IndexOf(">>") + 1
tmp = PDFData.Substring(StartIndex, EndIndex - StartIndex + 1)
'Now tmp="<< /Kids, /Count etc >>"
'the pagecount is just after "/Count " in tmp
CountIndex = tmp.IndexOf("/Count")
CountIndex += 7 'Move index to the end of "/Count "
tmp = tmp.Substring(CountIndex)
'now tmp="Pagecount ....>>"
'Pagecount is followd by a newline like char and then "/" or ">>"
CountEndIndex = tmp.IndexOfAny(chars)
tmp = tmp.Substring(0, CountEndIndex) 'Get the PageCount
If PageCount < Val(tmp) Then PageCount = Val(tmp)
PDFData = PDFData.Substring(EndIndex + 1)
Loop
MsgBox("# Pages = " & PageCount)
This code worked. If there any chance that I could also count .doc/ .docx page numbers?
-
Jan 28th, 2018, 03:12 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|