Populate pdf fields using vba
Copy link to clipboard
Copied
I'm trying to fill in an existing PDF file fields using access VBA by the code below. "User Name:" is one field on my pdf. I'm trying to enter a value to this field. When I run this with an appropriate file path it gives me an error saying: Run-time error '424': Object required. The Adobe file version is Adobe Acrobat XI Pro.
Private Sub Adobefill_Click()
Dim FileNm, gApp, avDoc, pdDoc, jso
FileNm = "C:\projects\test\"
Set gApp = CreateObject("AcroExch.app")
Set avDoc = CreateObject("AcroExch.AVDoc")
If avDoc.Open(FileNm, "") Then
Set pdDoc = avDoc.GetPDDoc()
Set jso = pdDoc.GetJSObject
jso.getfield("User Name:").Value = "[user name redacted by moderator by request]" '<-------- I get an error here
pdDoc.Save PDSaveIncremental, FileNm 'Save changes to the PDF document
pdDoc.Close
End If
'Close the PDF; the True parameter prevents the Save As dialog from showing
- avDoc.Close (True)
'Some cleaning
Set gApp = Nothing
Set avDoc = Nothing
Set pdDoc = Nothing
Set jso = Nothing
End Sub
Copy link to clipboard
Copied
Step through your code and make sure that you have a iso object. If that's the case, then the reason for the error is very likely that your document does not have a field named "User Name:" - make sure that you spell the field name exactly the same way as it is spelled in the PDF form.
Copy link to clipboard
Copied
Thank you for responding. I stepped through the code and that's how i got to know where the error is occuring. So if you don't mind how and where do i add the iso object?
Copy link to clipboard
Copied
The way you are doing it should work: You first get the AV doc, from that you get the PD doc, and from that you get the JSO. If the file is not being opened correctly, then you won't have an AVDoc, which means that the call to getPDDoc will also not return anything. You need to figure out where you are not getting the correct information. You know that at the point you want to use the JSO, it's not valid, but you need to work backwards to find out where things start to go wrong.
Copy link to clipboard
Copied
Does your field name actually include the colon “:”? That’s pretty unusual.
Copy link to clipboard
Copied
A field name CANNOT contain a ‘:’ – it’s forbidden by the PDF spec.
Copy link to clipboard
Copied
Leonard, you may want to tell that to the Acrobat team
The only time I usually see colons in a form field names is with forms that were created by Acrobat when it tried to identify form fields automatically, and then used whatever was in front of the assumed field as it's name (e.g. "User Name:"), including any colons.
Copy link to clipboard
Copied
Can you also help me with the code on how to select a value in a drop down menu or (Comobox) in the pdf?
Copy link to clipboard
Copied
You do that the same way as with a text field. Just make sure that the value you pass in is actually an available option in your drop down control.
Copy link to clipboard
Copied
Yup got it. Thanks!
Copy link to clipboard
Copied
Yes. It is ordered in this manner. The username is read only I cannot edit that.
User Name: (blank field to enter data)
Copy link to clipboard
Copied
The name of a field, and the label next to it, are not connected. Check the field name again. If you have managed to enter it with a colon, fix that.
Copy link to clipboard
Copied
Hi all,
It worked! I put it the exact field name and it worked. Thanks for all the help.
Copy link to clipboard
Copied
Look for the name of the blank field.
Copy link to clipboard
Copied
I need to populate pdf fields using vba code and the code works perfectly for me in my home computer where I have installed Acrobat Pro version. But when I run the application to my workplace it doesn't work because I have adobe reader installed there and I have no access to install anything there as a client machine. They have Office 365 installed and therefore I can run VBA but it doesn't work because Acrobat Pro is not available there. Is there any way to do the same task for Adobe reader or any possible solution to run the some application written at my home there?
Copy link to clipboard
Copied
You need Adobe Acrobat Standard or Pro.
Copy link to clipboard
Copied
I found there only AcroPDF.dll. Nothing else. So there is literally no way to access pdf fields without installing Standard or Pro version?
Copy link to clipboard
Copied
Adobe put there developer tools into the paid for software, and leave them out of the free software - because they are a marketing tool.
Copy link to clipboard
Copied
Karl, I've read several of your responses to the Auto Populate PDF files via VBA. First, thank you for your help. You are one of the only resources I've found on the topic. If there's any chance you still read comments here, I wanted to link my question.
Excel: 2016, 64 bit
OS: Windows 10
Adobe: Acrobat PRO DC
------------------------------------------------------------------------------------------------------------------------------
Previous attempt to accomplish this task:
I similarly have an excel file (.xlsm) running a VBA macro that uses columns E through AO, rows 4 through 14, to auto populate fillable fields throughout a 10 page PDF document, save the document, clear the form, and then begin filling in a brand new template (identical to the first) form using the second row (row 6) in excel, and so on.
However, this macro relied on senkeys and, while it worked perfectly on the first form all the way to saving it correctly and uploading to the correct folder, every following PDF would fill in inconsistently.
-------------------------------------------------------------------------------------------------------------------------------
Current attempt to accomplish this task:
I am now attempting to use the method that you have advised on here and in other posts and fill in form in by refferring to the PDF's field names and inserting the appropriate cell in excel (I have downloaded and pasted them to Sheet2 of my workbook). I am having trouble simply opening up the pdf, however, using the following code as you can see below:
When running the code pasted below, I receive the following message when it tries to open Adobe:
"There was an error opening this document. Access denied."
I downloaded the library using your directions in another post and made sure to reference everything in Excel as can be seen in the two images below:
Library Download:
References:
Current VBA Code: (My actual code includes the correct file path "C:\Users\......")
Option Explicit
Option Private Module
Sub OpenPDFPageView()
Dim PDFApp As AcroApp
Dim PDFDoc As AcroAVDoc
Dim pdDoc As Acrobat.CAcroPDDoc
Dim PDFPageView As AcroAVPageView
Dim PDFPath As String
Dim DisplayPage As Integer
Dim jso As Object
DisplayPage = 1
'Initialize Acrobat by creating App object
Set PDFApp = CreateObject("AcroExch.App")
'Set AVDoc object
Set PDFDoc = CreateObject("AcroExch.AVDoc")
'Set PDDoc object
Set pdDoc = CreateObject("AcroExch.PDDoc")
'Open the PDF
If PDFDoc.Open(PDFPath, "") = True And pdDoc.Open(PDFPath) = True Then
PDFDoc.BringToFront
'Maximize the document
Call PDFDoc.Maximize(True)
Set PDFPageView = PDFDoc.GetAVPageView()
'Go to the desired page
'The first page is 0
Call PDFPageView.GoTo(DisplayPage - 1)
'-------------
'ZOOM options
'-------------
'0 = AVZoomNoVary
'1 = AVZoomFitPage
'2 = AVZoomFitWidth
'3 = AVZoomFitHeight
'4 = AVZoomFitVisibleWidth
'5 = AVZoomPreferred
'Set the page view of the pdf
Call PDFPageView.ZoomTo(2, 50)
Set jso = pdDoc.GetJSObject
If Not jso Is Nothing Then
jso.Getfield("Offer Date").Value = "test"
End If
End If
Set PDFApp = Nothing
Set PDFDoc = Nothing
Set pdDoc = Nothing
On Error Resume Next
'Show the adobe application
PDFApp.Show
'Set the focus to adobe acrobat pro
AppActivate "Adobe Acrobat Pro"
End Sub
If you have any advice on what I'm doing wrong, I'd immensely appreciate it. Thank you so much for all of your help. You're the best!
Thomas.

