Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8134
  • Last Modified:

Fill in PDF form using MS-Access data

I'm trying to create a VBA routine to use my customer data in an MS-Access 2003 (mdb) database to populate a shipping request PDF form.  The problem is that I don't know the names of the fields in the PDF form.

So first I need a utility VBA routine that will simply iterate thru all the fields in the form to retrieve their names.

I've seen plenty of examples on how to populate the fields using something like:
jso.getField("topmostSubform[0].Page1[0].fieldName[0]").value = "myValue"
where jso is the JSObject from PDDoc object from AVDoc object.  There aren't any good coding examples using JSObject in VBA.

It seems the most closely guarded secret on the internet is how to use jso.getField("topmostSubform[0].Page1[0].fieldName[0]") more dynamically so that I can iterate thru all the fields in a PDF form using VBA code in Access.

I work for the Navy (DoD) so my machine is locked down and I cannot install any third party software.  I'm actually running Access 2010 but because the Navy has the machines so locked down and they really don't understand how to implement the Trust Center settings properly I have kept my db apps as .mdb to prevent security warnings and issues for my users.

Thank you for any suggestions and advice.
1 Solution
Jim P.Commented:
Does it absolutely have to populate the PDF form? Or could it be a simulation? And do you have access to a PDF printer?

Is there anything stopping you from make your own Access report and then "print" it to a PDF and send it off?

Just a dumb thought.
I work in a similarly constrained environment.  Our general approach to PDF forms is what Jim has suggested.  

ie:  Make a replica of the PDF form as an Access Report, and use the OutputTo command with acFormatPDF as the format parameter to export the report to PDF (and yes, this does work in Access 2010 with databases in .mdb format).

That said, to loop through fields in a PDF form, you should be able to treat it like any other collection in VBA.  

I haven't tested this myself, but look at the lines labeled  "borrowed code" in this Adobe Forums post:

I'm *guessing*, based on Access's fields collection, that this would also work for PDFs (assumption: you have already declared 'JSO"):

     Dim fld

     For Each fld In JSO.Fields
           Debug.Print "Field Name: " & fld.Name & "  Value: " & fld.Value

Open in new window

Karl Heinz KremerCommented:
Based on the path for your form fields, you are using XFA forms (forms created in LiveCycle Designer, not AcroForms). To get the list of fields, you have to do some extra work.

One approach is to open up your document in Adobe Acrobat, then open up the JavaScript console (Ctrl-J or Cmd-J), then execute the following line of code:;

Open in new window

This will reply with an XML data structure that shows you all the fields:

<?xml version="1.0" encoding="UTF-8"?>
<xfa:data xmlns:xfa=""

Open in new window

Now you should be able to figure out what the field names are and how to access them.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Asteroid_MiniicAuthor Commented:
Thanks all, for your input.  Sorry for the late response.

I cannot do an Access report and print to PDF printer (driver) because the PDF form is a standard shared form meaning we need to put our shipping data on the certain fields on the form and then the form is sent electronically to the shipping department and they fill in other fields on the form, which is then send to another organization who fills in the their part on the same form (the govt is a boon-doggle when it comes to processing anything--sigh).

Good news, I'm mostly there.  I found a way to retrieve the field names from a PDF form and a way to populate the form with my Access data.  I'm not at work right now, so I'll post the code later.  I just need to figure a way to keep the blank form intact by keeping it hidden (protected) from users, and have each user using a copy as their own form.  Not too hard.
Asteroid_MiniicAuthor Commented:
This routine will read through the fields in a PDF form and and put the name of the field as the value of the field, and then save it as a new form so you can see the field names.  WARNING: You most likely will get errors within Adobe using this routine because of the format of some fields (e.g. Date and numeric).  If this happens, simply switch to the Adobe window and click through the warnings (maybe take note of them).  You should have the following libraries (or some version thereof) included in the References: Adobe Acrobat 10.0 Type Library and AFormAut 1.0 Type Library.  The IF statements are just to avoid some Adobe warnings and are unique to my situation.   Also missing is the step to populate the form with data from the database, but I assume anyone reading this will know how to retrieve data from their database.

Here is the code:
Asteroid_MiniicAuthor Commented:

Private Sub test_Adobe()

Dim jsFile As String
Dim joApp As AcroApp
Dim joAVDoc As AcroAVDoc
Dim joPDDoc As Acrobat.AcroPDDoc
Dim joFormApp As AFORMAUTLib.AFormApp
Dim joFormFields As AFORMAUTLib.Fields
Dim joFormField As AFORMAUTLib.Field

    On Error GoTo Error_Handler
    jsFile = "C:\Users\someuser\Documents\Projects\EXWC_SHIPPING_REQUEST.pdf"
    Set joApp = New AcroApp
    Set joAVDoc = CreateObject("AcroExch.AVDoc")
    If joAVDoc.Open(jsFile, "") Then
        Set joPDDoc = joAVDoc.GetPDDoc()
        Set joFormApp = CreateObject("AFormAut.App")
        Set joFormFields = joFormApp.Fields
        For Each joFormField In joFormFields
            If joFormField.Type = "text" Then
                If Left(joFormField.Name, 3) <> "TC " _
                And Left(joFormField.Name, 6) <> "PRICE " _
                    joFormField.Value = joFormField.Name	'<-- this is the main line of code
                End If
                If joFormField.Name = "FUNDS TYPE" Then
                    joFormField.Value = "NWCF"
                End If
                If joFormField.Name = "SHIPMENT MODE" Then
                    joFormField.Value = "Ground"
                End If
            End If

        Next joFormField
        jsFile = "C:\Users\someuser\Documents\Projects\EXWC_SHIPPING_REQUEST1.pdf"
        joPDDoc.Save PDSaveFull, jsFile
    End If

    joAVDoc.Close True
    Set joPDDoc = Nothing
    Set joAVDoc = Nothing
    Set joApp = Nothing
    MsgBox "Done processing"
    Exit Sub
    MsgBox Err.Number & ": " & Err.Description, vbCritical, "Adobe Testing"
    GoTo Exit_Handler

End Sub

Open in new window

Jim P.Commented:
I'm glad you resolved your issue. I'm going to add this to my personal knowledge base for future use.

Thanks for using EE.
Asteroid_MiniicAuthor Commented:
Jim P, you're welcome.  Please share with anyone who gets stuck on this issue.

Asteroid_MiniicAuthor Commented:
Solves my problem.
frankie cunesCommented:
Helpful suggestions . For my two cents if others are requiring a Taiwan Synology Developer Guide File Hosting Module , my family came across a fillable document here
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now