We start the same way as in my old VBA sample to create a VBA program that references the Acrobat TLB and to add a button to a document. When we now use the following script as the button handler, we can work with form fields:
Private Sub CommandButton1_Click() Dim AcroApp As Acrobat.CAcroApp Dim theForm As Acrobat.CAcroPDDoc Dim jso As Object Dim text1, text2 As String Set AcroApp = CreateObject("AcroExch.App") Set theForm = CreateObject("AcroExch.PDDoc") theForm.Open ("C:\temp\sampleForm.pdf") Set jso = theForm.GetJSObject ' get the information from the form fields Text1 and Text2 text1 = jso.getField("Text1").Value text2 = jso.getField("Text2").Value MsgBox "Values read from PDF: " & text1 & " " & text2 ' set a text field Dim field2 As Object Set field2 = jso.getField("Text2") field2.Value = 13 ' assign the number 13 to the fields value ' get the information from the form fields Text1 and Text2 text1 = jso.getField("Text1").Value text2 = jso.getField("Text2").Value MsgBox "Values read from PDF: " & text1 & " " & text2 theForm.Close AcroApp.Exit Set AcroApp = Nothing Set theForm = Nothing MsgBox "Done" End Sub
This program requires a PDF file with text fields called “Text1” and “Text2” to be stored as C:\temp\sampleForm.pdf. With the explanation in the previous two blog posts, it should not be hard to understand what’s going on here. The only new command introduced is the getField() function, which returns a form field. The form field object has a property “value” which contains the actual value that’s assigned to the field. Give it a try and let me know how it works for you. The updated form field is not saved (because the document does not get saved) – I’ll leave that up to the reader to figure out.
Also, this program will not work with XFA forms (the ones you create in Designer). For those, you need to use the XFA DOM to access the form data. For anybody interested in XFA forms, the LifeCycle Designer ES Scripting Reference is a must read.