Hello Friends,
i have multiple pdf files and want to extract the pages count of each individual pdf.
is it possible in macro?
Please suggest me
i want 2 details of each file
i.e. 1) FileName, 2) Pages
Hello Friends,
i have multiple pdf files and want to extract the pages count of each individual pdf.
is it possible in macro?
Please suggest me
i want 2 details of each file
i.e. 1) FileName, 2) Pages
Hi Mohan,
check out this attached file and let me know is it for your looking for
Thanks - Naveed
-----------------------------
If the suggestion helps you, then Click * to Add Reputation
To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.
Hello Naveed,
thanks for reply
i run your code and found this error
also fileName on sheet1 is ok but page count of each file is 0 which wrong![]()
If sht.Range(file, iExtLen) = sExt Then
AOA Naveed,
found the error "Run-time error '75': while running the macro with your shared excel file.
Hello Naveed,
thanks for reply
i run your code and found this error
also fileName on sheet1 is ok but page count of each file is 0 which wrong![]()
If sht.Range(file, iExtLen) = sExt Then
Hello Naveed,
thanks for reply
i run your code and found this error
also fileName on sheet1 is ok but page count of each file is 0 which wrong![]()
If sht.Range(file, iExtLen) = sExt Then
Hi mohan
replace below code
![]()
Sub PDFandNumPages() Dim Folder As Object Dim file As Object Dim fso As Object Dim iExtLen As Integer, iRow As Integer Dim sFolder As String, sExt As String Dim sPDFName As String sExt = "pdf" iExtLen = Len(sExt) iRow = 2 ' Must have a '\' at the end of path sFolder = "C:\Users\NaveedM\Downloads\Excel Forum\pdf\" Set fso = CreateObject("Scripting.FileSystemObject") If sFolder <> "" Then Set Folder = fso.GetFolder(sFolder) For Each file In Folder.Files If Right(file, iExtLen) = sExt Then Cells(iRow, 1).Value = file.Name Cells(iRow, 2).Value = pageCount(sFolder & file.Name) iRow = iRow + 1 End If Next file End If End Sub Function pageCount(sFilePathName As String) As Integer Dim nFileNum As Integer Dim sInput As String Dim sNumPages As String Dim iPosN1 As Integer, iPosN2 As Integer Dim iPosCount1 As Integer, iPosCount2 As Integer Dim iEndsearch As Integer ' Get an available file number from the system nFileNum = FreeFile 'OPEN the PDF file in Binary mode Open sFilePathName For Binary Lock Read Write As #nFileNum ' Get the data from the file Do Until EOF(nFileNum) Input #1, sInput sInput = UCase(sInput) iPosN1 = InStr(1, sInput, "/N ") + 3 iPosN2 = InStr(iPosN1, sInput, "/") iPosCount1 = InStr(1, sInput, "/COUNT ") + 7 iPosCount2 = InStr(iPosCount1, sInput, "/") If iPosN1 > 3 Then sNumPages = Mid(sInput, iPosN1, iPosN2 - iPosN1) Exit Do ElseIf iPosCount1 > 7 Then sNumPages = Mid(sInput, iPosCount1, iPosCount2 - iPosCount1) Exit Do ' Prevent overflow and assigns 0 to number of pages if strings are not in binary ElseIf iEndsearch > 1001 Then sNumPages = "0" Exit Do End If iEndsearch = iEndsearch + 1 Loop ' Close pdf file Close #nFileNum pageCount = CInt(sNumPages) End Function
Hello Naveed,
now the last line of pageCount function having problem
run-time error no. 13
![]()
pageCount = CInt(sNumPages)
The code below works for me and correctly puts the file name in column A with pages in column B
I have tried it with Excel2010 and Excel2016 and BOTH work
I am also using Windows7
Try my file attached - amend the folder path, then run PDFandNumPages
![]()
Sub PDFandNumPages() Dim Folder As Object Dim file As Object Dim fso As Object Dim iExtLen As Integer, iRow As Integer Dim sFolder As String, sExt As String Dim sPDFName As String sExt = "pdf" iExtLen = Len(sExt) iRow = 2 ' Must have a '\' at the end of path sFolder = "D:\Documents\TestFolder\" Set fso = CreateObject("Scripting.FileSystemObject") If sFolder <> "" Then Set Folder = fso.GetFolder(sFolder) For Each file In Folder.Files If Right(file, iExtLen) = sExt Then Cells(iRow, 1).Value = file.Name Cells(iRow, 2).Value = pageCount(sFolder & file.Name) iRow = iRow + 1 End If Next file End If End Sub Function pageCount(sFilePathName As String) As Integer Dim nFileNum As Integer Dim sInput As String Dim sNumPages As String Dim iPosN1 As Integer, iPosN2 As Integer Dim iPosCount1 As Integer, iPosCount2 As Integer Dim iEndsearch As Integer ' Get an available file number from the system nFileNum = FreeFile 'OPEN the PDF file in Binary mode Open sFilePathName For Binary Lock Read Write As #nFileNum ' Get the data from the file Do Until EOF(nFileNum) Input #1, sInput sInput = UCase(sInput) iPosN1 = InStr(1, sInput, "/N ") + 3 iPosN2 = InStr(iPosN1, sInput, "/") iPosCount1 = InStr(1, sInput, "/COUNT ") + 7 iPosCount2 = InStr(iPosCount1, sInput, "/") If iPosN1 > 3 Then sNumPages = Mid(sInput, iPosN1, iPosN2 - iPosN1) Exit Do ElseIf iPosCount1 > 7 Then sNumPages = Mid(sInput, iPosCount1, iPosCount2 - iPosCount1) Exit Do ' Prevent overflow and assigns 0 to number of pages if strings are not in binary ElseIf iEndsearch > 1001 Then sNumPages = "0" Exit Do End If iEndsearch = iEndsearch + 1 Loop ' Close pdf file Close #nFileNum pageCount = CInt(sNumPages) End Function
Last edited by kev_; 06-10-2017 at 10:35 AM.
Click *Add Reputation to thank those who helped you. Ask if anything is not clear
Thanks for reply
i tried your code but this time i get all filename in column A but in B Column Pagecount shows 0.
where i am getting wrong, please suggest
Hi,
i just make one changes in your code i.e Path
rest of all the code is same for me.
Hi Kev,
I have no code if you see my first post.
i required two 2 things from pdf file i.e. File Name and Pages
as per your code i get only file name correctly but page count shows 0.
i check your code on another PC where Acrobat professional 7.0 version installed and found OK.
thats why i made last post that "i am using Acrobat Reader 9.0 version of pdf."
now we know that its a pdf version problem so is there any chance to get page count in Acrobat reader version?
Please suggest.
How do you expect me to work that out when you provide no information?![]()
![]()
Please post your modified code enclosed in code tags
thanks
Hi Kev,
i am using Acrobat reader 9.0 version of pdf
Please post your code as requested
Also attach an example pdf file
thanks
(the code works for me with all pdf's on my computer, but your pdf file may have a different structure???)
Last edited by kev_; 07-04-2017 at 06:48 AM.
Please attach 2 pdf files for me to test
- pdf(1) = where page count = 0
- pdf(2) = where page count > 0
Hi Kev,
there is no problem in pdf, the problem is in version.
if you have Adobe Reader 9.0 version of pdf then you can also check
Try modifying the code. Please add one line of code after this line:
add this line:![]()
ElseIf iEndsearch > 1001 Then
Does the message pop up when you run the code on the "bad" machine?![]()
MsgBox "Overflow tripped"
Hi KEV,
yes,
i got the msg "Overflow tripped"
post deleted by kev_
Last edited by kev_; 07-05-2017 at 05:23 AM.
I have amended the macro to provide me with extra information on sheet named "sInput" in attached workbook
Please:
1 amend this line in the macro to match a folder on your system
2 Put 10 pdf files in that folder![]()
sFolder = "D:\Documents\TestFolder\"
3 run the macro
4 manually enter the number of pages against each file in column B on sheet FileList
5 attach the workbook to your reply
thanks
Last edited by kev_; 07-05-2017 at 05:23 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks