VBA Adobe Acrobat
Adobe Acrobat is software used to work with PDFs. PDF is Portable Document Format and was created by Adobe. PDF format is used to present documents. The Adobe Acrobat 10.0 Type Library can be used with VBA to programmatically work with Adobe Acrobat.
Getting Started
To use VBA with Adobe Acrobat, Adobe Acrobat must be purchased and installed on the user's computer. Installing Adobe Acrobat should give VBA access to the Adobe Acrobat 10.0 Type Library. Set a project reference to: Adobe Acrobat 10.0 Type Library (C:\Program Files (x86)\Adobe\Acrobat 2017\Acrobat\acrobat.tlb).
Another option instead of developing custom Adobe Acrobat solutions using VBA is to use a commercial plug-in like AutoMailMerge.
Object Model
The Acrobat Object Model consists of two layers: the application or AV layer, and the document or PD layer. The AVApp object is the top-level object representing the Adodbe Acrobat application. The AVDoc object represents a window containing an open PDF. The PDDoc object represents a PDF document. In addition to these layers, there is an object called the JSObject which is used to interop with the PDF JavaScript API.
Application Layer
The AV application layer provides objects for working with the window used to view PDF documents.
Object | Description |
---|---|
AVApp | The top-level object which represents the Adobe Acrobat application. Can control the appearance and size of the application window and can be used to access the menu bar and tool bar of the application. |
AVDoc | Represents a window containing an open PDF document. |
AVPageView | Used for scrolling, magnification, changing pages, and accessing history. |
AVMenu | Represents a menu. |
AVMenuItem | Represents a single item in a menu. |
AVConversion | Represents the format to save the document. |
Document Layer
The PD document layer provides objects for working with PDF documents.
Object | Description |
---|---|
PDDoc | Represents a PDF document. |
PDPage | Represents a single page of a PDF document. |
PDAnnot | Represents an annotation. |
PDBookmark | Represents a Bookmark |
PDTextSelect | Represents selected text. |
JSObject
The native scripting language for PDF format is JavaScript. There is a JavaScript API for developers to work with PDFs. In VBA, the JSObject is used to access the JavaScript API. VBA does not have access to everything in the JavaScript API but most reasonable tasks can be accomplished. For learning the JavaScript API and using VBA with Adobe Acrobat see the JavaScript API Reference and the IAC Developer Guide provided by Adobe with the Adobe Acrobat SDK.
Example
This example should provide a basic template for automating filling out a PDF form.
Option Explicit
Private Function GetAVApp() As Object
On Error Resume Next
Set GetAVApp = GetObject("", "AcroExch.App")
On Error GoTo 0
If GetAVApp Is Nothing Then
Set GetAVApp = CreateObject("AcroExch.App")
End If
End Function
Public Sub AutomateAdobeAcrobatPDF()
'''Requires a reference to Adobe Acrobat 10.0 Type Library
Dim aApp As Acrobat.AcroApp
Dim aAVDoc As Acrobat.AcroAVDoc
Dim aPDDoc As Acrobat.AcroPDDoc
Dim JSO As Object 'There is no explicit class type for the JSObject
'Set PDF template file path
Dim TemplatePath As String
TemplatePath = "C:\PDFTemplate.pdf"
'Get Acrobat Application
Set aApp = GetAVApp()
'Use the AV layer document object to control how the document is viewed
Set aAVDoc = CreateObject("AcroExch.AVDoc")
If Not aAVDoc.Open(TemplatePath, "") Then
MsgBox "There was a problem opening template: " & TemplatePath, vbCritical
Exit Sub
End If
'Use the PD layer document object to access info about the document
Set aPDDoc = aAVDoc.GetPDDoc
'Use the JSObject object to access PDF JavaScript API
Set JSO = aPDDoc.GetJSObject
'''Do something''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
'Check a checkbox
JSO.getField("Checkbox1").checkThisBox 0, True
'Add a signature from an image file
'This is a workaround - add a button and add image to button
Dim SigRect(3) As Single
SigRect(0) = 400
SigRect(1) = 65
SigRect(2) = 500
SigRect(3) = 30
JSO.addField "sigbutton", "button", 0, SigRect
JSO.getField("sigbutton").buttonPosition = 2
JSO.getField("sigbutton").buttonImportIcon "Signature.png"
JSO.getField("sigbutton").buttonFitBounds = True
'Add Date
Dim DateRect(3) As Single
DateRect(0) = 360
DateRect(1) = 55
DateRect(2) = 405
DateRect(3) = 42
JSO.getField("date").rect = DateRect
JSO.getField("date").Value = Format$(Date$, "dd/mm/yyyy")
JSO.getField("date").TextSize = 8
'''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
'Close Docs
aPDDoc.Close
aAVDoc.Close False
'Exit Adobe
aApp.Exit
'Cleanup
Set JSO = Nothing
Set aPDDoc = Nothing
Set aAVDoc = Nothing
Set aApp = Nothing
End Sub