+ Reply to Thread
Results 1 to 21 of 21

Extract Page count of pdf in macro

Hybrid View

  1. #1
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Extract Page count of pdf in macro

    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

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Extract Page count of pdf in macro

    Hi Mohan,

    check out this attached file and let me know is it for your looking for
    Attached Files Attached Files
    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.

  3. #3
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hello Naveed,

    thanks for reply

    i run your code and found this error
     If sht.Range(file, iExtLen) = sExt Then
    also fileName on sheet1 is ok but page count of each file is 0 which wrong

  4. #4
    Registered User
    Join Date
    04-13-2018
    Location
    Pakistan
    MS-Off Ver
    2013
    Posts
    3

    Re: Extract Page count of pdf in macro

    AOA Naveed,

    found the error "Run-time error '75': while running the macro with your shared excel file.

  5. #5
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hello Naveed,

    thanks for reply

    i run your code and found this error
     If sht.Range(file, iExtLen) = sExt Then
    also fileName on sheet1 is ok but page count of each file is 0 which wrong

  6. #6
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hello Naveed,

    thanks for reply

    i run your code and found this error
     If sht.Range(file, iExtLen) = sExt Then
    also fileName on sheet1 is ok but page count of each file is 0 which wrong

  7. #7
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: Extract Page count of pdf in macro

    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

  8. #8
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hello Naveed,

    now the last line of pageCount function having problem

    run-time error no. 13
    pageCount = CInt(sNumPages)

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    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
    Attached Files Attached Files
    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

  10. #10
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    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

  11. #11
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hi,

    i just make one changes in your code i.e Path
    rest of all the code is same for me.

  12. #12
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    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.

  13. #13
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    How do you expect me to work that out when you provide no information?

    Please post your modified code enclosed in code tags

    thanks

  14. #14
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hi Kev,

    i am using Acrobat reader 9.0 version of pdf

  15. #15
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    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.

  16. #16
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    Please attach 2 pdf files for me to test
    - pdf(1) = where page count = 0
    - pdf(2) = where page count > 0

  17. #17
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    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

  18. #18
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    Try modifying the code. Please add one line of code after this line:
    ElseIf iEndsearch > 1001 Then
    add this line:
    MsgBox "Overflow tripped"
    Does the message pop up when you run the code on the "bad" machine?

  19. #19
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Extract Page count of pdf in macro

    Hi KEV,

    yes,
    i got the msg "Overflow tripped"

  20. #20
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    post deleted by kev_
    Last edited by kev_; 07-05-2017 at 05:23 AM.

  21. #21
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Extract Page count of pdf in macro

    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
    sFolder = "D:\Documents\TestFolder\"
    2 Put 10 pdf files in that folder

    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
    Attached Files Attached Files
    Last edited by kev_; 07-05-2017 at 05:23 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I am looing for a macro to extract the title of a web page from a URL
    By emymeeky in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-28-2015, 04:32 PM
  2. [SOLVED] Extract and count data - macro
    By missy22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2014, 07:07 AM
  3. Automatically adding a blank page at the end of sheets with page count odd
    By NewDawn999 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-15-2013, 02:33 PM
  4. Extract Data from a Web Page - using VBA
    By Tejas.T in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2012, 03:17 PM
  5. Extract only numbers from one cell to another page?
    By sixout in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-16-2012, 12:13 PM
  6. Replies: 2
    Last Post: 08-04-2011, 06:42 AM
  7. Inserting page number & page count in cells
    By Jeff in forum Excel General
    Replies: 0
    Last Post: 05-02-2005, 04:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1