Search code examples
vbaexcelpdfacrobat

How to get the number of pages in a pdf document using VBA?


I will post my solution to this question, but maybe others have found a better way.

I wanted to obtain the number of pages in a pdf document using VBA.

I reviewed similar [vba] and [acrobat] questions, but I did not find a stand alone solution. After reviewing other posts, Adobe Acrobat's SDK, and the VBA object browser, I learned enough to piece together this solution.

I am running Excel 2013 and Adobe Acrobat 9.0 Pro.

I understand its ok to answer my own question.


Solution

  • This solution works when Excel 2013 Professional and Adobe Acrobat 9.0 Pro are installed.

    You will need to enable the Adobe object model: Tools -> References -> Acrobat checkbox selected.

    Adobe's SDK has limited documentation on the GetNumPages method.

    'with Adobe Acrobat 9 Professional installed
    'with Tools -> References -> Acrobat checkbox selected
    
    Sub AcrobatGetNumPages()
    
    Dim AcroDoc As Object
    
    Set AcroDoc = New AcroPDDoc
    
    AcroDoc.Open ("C:\Users\Public\Lorem ipsum.pdf") 'update file location
    
    PageNum = AcroDoc.GetNumPages
    
    MsgBox PageNum
    
    AcroDoc.Close
    
    End Sub