Skip to main content
Participating Frequently
January 13, 2010
Question

Opening and populating a PDF form with VBA code in Access 2007

  • January 13, 2010
  • 1 reply
  • 46042 views

I had posted this in Acrobat Windows.  It was suggested this (and the SDK forum) may be a better forum ...

I'm trying to load and then populate a PDF form programmatically using Access/VBA.  I'm patterning this after code that worked fine in Acrobat 5.0 but is throwing errors in Acrobat 9.  We're using Access 2007 on Windows XP and Vista computers.

The Access project has a reference to Adobe Acrobat 9.0 Type Library.  Attached is a jpg showing all the references in the Access project.

The error "Object variable not set (Error 91)" is happening with this statement:
Set PDDoc = AVDoc.GetPDDoc


Here is the code I'm trying to use.  I've always been a little fuzzy exactly which objects need to be created and the sequence they need to be created in.  Once I got it working in Acrobat 5 I left it alone.

    Dim WshShell As Object
    Dim myApp As Acrobat.AcroApp
    Dim AVDoc As Acrobat.AcroAVDoc
    Dim PDDoc As Acrobat.AcroPDDoc
    Dim PauseTime, Start

    Set WshShell = CreateObject("Wscript.Shell")
    ' run the Acrobat application within that shell and pass it a document name
    WshShell.Run "Acrobat.exe C:\Users\Christian\Documents\data\dist5\dist05_face_only_nh.pdf"
   
   
    '// Set/Get Acrobat Objects
    ' create an automation object that references the active copy of Acrobat
    Set myApp = CreateObject("AcroExch.App")
    ' reference the acrobat document we loaded above
    'Set AVDoc = CreateObject("AcroExch.AVDoc")
    Set AVDoc = myApp.GetActiveDoc()
   
    ' this apparently runs some method available to AVDOC
    Set PDDoc = AVDoc.GetPDDoc      '<------------------ THIS IS THE LINE THROWING THE ERROR
    ' this is how you reference the JSObject
    Set jso = PDDoc.GetJSObject
   
    ' let's clear the form
    temp = jso.resetForm()

   
    Set x = jso.getField("txt1_name")
    x.Value = "Bing Crosby"
   
    Set x = jso.getField("txt2_sex")
    x.Value = "Male"
   
    Set WshShell = Nothing
    Set myApp = Nothing
    Set AVDoc = Nothing
    Set PDDoc = Nothing

Thanks in advance for any help.

Christian Bahnsen

This topic has been closed for replies.

1 reply

lrosenth
Adobe Employee
Adobe Employee
January 13, 2010

How about downloading the Acrobat SDK and reading the documentation to understand what you've written?!?!

Participating Frequently
January 13, 2010

I'll give that another shot.  Hopefully the SDK has matured since last I perused it (back in version 5).  It was pretty sketchy then.  I recently got Acrobat 9 (mainly because I bought a laptop running Vista and my venerable version 5 wouldn't load thereon).  Does Adobe have the equivalent of MSDN?  I recall being very frustrated at the time I built the first application using the code above because I couldn't find code samples to test.

Thanks for your reply.

Participating Frequently
January 18, 2010

About point 1) I really wonder. For me it works without problems.

However I attached another example which starts the file from script

and then get the AVDoc from already opened file in Acrobat.

For the release of the object I don't take really care.

If VBS closed and Acrobat closed all objects released by standard.

Enjoy, Reinhard

PS: The attached script gather all fields and report them or report that no field found.

AcroGetFields.vbs

------------------------------------------------------------------------

set WshShell = CreateObject ("Wscript.Shell")
WshShell.run "Acrobat.exe c:\Test.pdf"
While not WshShell.AppActivate("Adobe Acrobat") : Wscript.Sleep 1000 : Wend
Wscript.Sleep 1000

  '// Set/Get Acrobat Objects
Set App = CreateObject("AcroExch.App")
Set AVDoc = App.GetActiveDoc
Set PDDoc = AVDoc.GetPDDoc
Set JSO = PDDoc.GetJSObject

  '// search for fields and get Name+value
txt = ""
if jso.numFields then
  for i = 0 to jso.numFields -1
     fn = jso.getNthFieldName(i)
     set f = jso.getField(fn)
     fv = f.value
     txt = txt  &fn &"=" &fv &";"
  next
  msgbox(txt)
else
  msgbox("No fields found")
end if

Set AVDOC = Nothing
Set PDDoc = Nothing
Set JSO = Nothing


Reinhard,

Thanks again for your help.

I tweaked your sample, mainly changing the way we delay while the Acrobat form loads.  The end product is very close to my original post.  Here what works for me:

Private Sub cmdHybrid_Click()

    Dim WshShell As Object
   
    Set WshShell = CreateObject("Wscript.Shell")
   
    WshShell.Run "Acrobat.exe C:\Users\Christian\Documents\data\dist5\dist05_face_only_nh.pdf"

    ' delay to let the form load completely
    PauseTime = 1
    Start = Timer
    Do While Timer < Start + PauseTime
        DoEvents
    Loop
   
    Set App = CreateObject("Acroexch.app")
    Set avDoc = CreateObject("AcroExch.AVDoc")
    Set avDoc = App.GetActiveDoc
    Set pdDoc = avDoc.GetPDDoc
    Set jso = pdDoc.GetJSObject
   
   
    ' let's clear the form
    temp = jso.resetForm()
   
    ' populate a couple fields
    Set x = jso.getField("txt1_name")
    x.Value = "Jerry Lewis"
   
    Set x = jso.getField("txt2_sex")
    x.Value = "Male"
   
    'housekeeping on the way out
    Set App = Nothing
    Set avDoc = Nothing
    Set pdDoc = Nothing
    Set jso = Nothing
   
   
End Sub

Adobe pros: any changes you'd suggest, or does this look kosher to you, too?