Fill in PDF form using MS-Access data

Posted on 2014-04-02
Medium Priority
1 Endorsement
Last Modified: 2016-09-09
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.
Question by:Asteroid_Miniic
LVL 38

Expert Comment

by:Jim P.
ID: 39973098
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.
LVL 61

Expert Comment

ID: 39974614
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

LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 39975603
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="http://www.xfa.org/schema/xfa-data/1.0/"

Open in new window

Now you should be able to figure out what the field names are and how to access them.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 39990568
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.

Accepted Solution

Asteroid_Miniic earned 0 total points
ID: 40019021
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:

Author Comment

ID: 40019027

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

LVL 38

Expert Comment

by:Jim P.
ID: 40019071
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.

Author Comment

ID: 40021870
Jim P, you're welcome.  Please share with anyone who gets stuck on this issue.


Author Closing Comment

ID: 40026812
Solves my problem.

Expert Comment

by:frankie cunes
ID: 41790846
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 http://goo.gl/3zl7eV.

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question