Adobe Acrobat and VBA – An Introduction
Posted by khk
Here is another topic that comes up every now and then: How can I “talk” to Adobe Acrobat MS Excel via VBA? I’ll try to give an introduction into that subject in this document. I will only discuss the basics, but I’m open for suggestions about what part to discuss next. So keep the comments coming.
More after the jump…
The Warning Upfront
Before we get too deep into this, let me say this: I am not a VBA expert. I do not program in VBA or VB. All I know about VB is from googling a few things and looking at sample code. It does help that I’ve programmed in many (make that a capital ‘M’ Many) programming languages, and at the end most of them share enough characteristics that once you know one, you know all of them… But still, don’t consider my VB programs to be at an expert leve
l. I only u the samples to demonstrate general methods. It’s up to you to fill in all the missing details (e.g. exception handling).
Resources
中国传统哲学All this information is available in one form or another in Adobe’s SDK documentation. Before you read any further, click on this link and take a look at what they have available.
There are (at least) two documents that are required reading if you want to u Acrobat from within your VBA code:
∙Developing Applications Using Interapplication Communication (510KB PDF Document)
∙Interapplication Communication API Reference司徒是什么官职 (2.3MB PDF Document)
补气血的汤If you want to utilize the VB/JavaScript bridge, you also should read the JavaScript related documents:
∙JavaScript for Acrobat API Reference – Version 8 (7.5MB PDF document)
∙Developing Acrobat Applications Using JavaScript (2.7MB PDF document)
关于朋友的名言警句All of the documents can also be accesd via Adobe’s online documentation system. In order to find the documents I’ve listed above, you need to expand the tree on the left side of the window for the “JavaScript” and “Acrobat Interapplication Communication” nodes.
There is always more than one way…
There are two ways your program can interact with Acrobat. One is more direct than the other, but both require the same mechanism to get things started…
You can either u the “normal” IAC (Inter Application Communication) interface, which is basically a COM object that your program loads and us to communicate with Acrobat, or you can u the VB/JavaScript bridge, which allows access to Acrobat’s JavaScript DOM. The latter ca still requires that your program first establishes a connection to Acrobat via IAC.
冬天怎么钓鲫鱼
Let’s get the party started
As I mentioned before, regardless of how we want to remote control Adobe Acrobat from VB, we need to establish a connection to it’s COM object (or OLE rver). You may have noticed that I always talk about “Adobe Acrobat”, and not the “Adobe Reader”. What I’m prenting here is valid for the Adobe Acrobat, Reader only supports a small subt of features. To learn more about what the differences are, e the IAC Developer Guide. For the purpo of this document, I will u MS Excel 2007 and Adobe Acrobat 9 Pro. As long as you have a version of Acrobat that is compatible with the version of VBA that you are using, you should be able to follow along without any problems.
Preparing MS Excel 2007
When you install Office 2007 or Excel 2007, make sure that you lect the Visual Basic Editor component, otherwi you will not be able to write VBA code. This is different than all the versions up to 2007. Once installed, you need to add the “Developer” tab to the ribbon. This is done on the Excel Options dialog, under the Popular category:
侵蚀地貌Once that is done, you should e the “Developer” tab as part of the ribbon:
Our First Button
Open a new document and lect the Developer tab. Then go to the Inrt control and pl
ace a button on your document. This will pop up the “Assign Macro” dialog, just click on the “Add” button, which will bring up the VBA editor. Nothing special so far.
Before we can u any of Acrobat’s functionality, we need to make sure that VBA knows about the Acrobat objects. On the VBA dialog, lect the “Tools>References” menu item. On the dialog that pops up, make sure that the TLB for your version of Acrobat is lected. This is what it looks like for my system:
Now we can add code that references the Acrobat objects to our button handler. Of cours
e, before we do that, we need to decide what our button is actually suppod to trigger. Let’s start with something simple – let’s combine two PDF documents and save the result as a new document.
I’ll prent the whole program first, and will then explain the different parts.豌豆怎么种
Sub Button1_Click()
Dim AcroApp As Acrobat.CAcroApp
Dim Part1Document As Acrobat.CAcroPDDoc
Dim Part2Document As Acrobat.CAcroPDDoc
Dim numPages As Integer
Set AcroApp = CreateObject("AcroExch.App")
抖音短句 Set Part1Document = CreateObject("AcroExch.PDDoc")
Set Part2Document = CreateObject("AcroExch.PDDoc")
Part1Document.Open ("C:\temp\Part1.pdf")
Part2Document.Open ("C:\temp\Part2.pdf")
' Inrt the pages of Part2 after the end of Part1