Home > Back-end >  Cannot get VBA code to check pdf check box
Cannot get VBA code to check pdf check box

Time:03-15

I am trying to make a program that reads one pdf and a vba user form and then fills out another pdf. I successfully wrote code to read all text in a pdf and then find certain sub strings based on tokens that I can find in the string. It is intended to populate the fields in the destination pdf based on the substrings and check the appropriate text boxes based on the user form. I can get the code to fill the substrings and then save the document, but it wont check the boxes. Before the code used a AVDoc, but I switched to a JSO because I dont want the pdf to pop up, and the jso successfully avoids that problem. I have tried various code approaches, such as pdfBool.value = cBool(vbaBool), pdfBool.value = 1, pdfBool.value = "1", jso.setValue("checked"), jso.setValue("yes"), etc. This code will run without crashing. I have reduced the number of variables to just one string and one bool for the sake of the example.

Sub main()

‘findString grabs all text from a pdf file.  This code works.
Dim mystr As String
If findString(mystr) = False Then
    Application.StatusBar = "Cannot find Source PDF"
    Exit Sub
End If

Dim mypath As String
    mypath = ActiveWorkbook.Path & "\destination.pdf"
 
Dim aApp As acrobat.AcroApp
Dim pdfDoc As acrobat.CAcroPDDoc
Dim jso As Object
   
    Set aApp = CreateObject("AcroExch.App")
    Set pdfDoc = CreateObject("AcroExch.PDDoc")

    If pdfDoc.Open(mypath) = True Then

            Set jso = pdfDoc.GetJSObject

        Dim vbaText As String
        Dim vbaBool As String

            vbaText = returnString("Token1")
            vbaBool = userForm.checkBox1.value

        Dim pdfText As Object
        Dim pdfBool As Object

            Set pdfText = jso.getField("TextField1")
            Set pdfBool = jso.getField("CheckBox1")

           pdfText.Value = vbaText
           pdfBool.Value = vbaBool

           'save pdffile
           Dim fileSavePath As String
           fileSavePath = ActiveWorkbook.Path & "\My Save File.pdf"
           pdfDoc.Save PDSaveFull, fileSavePath

           ‘clean up memory

           Set pdfDoc = Nothing
           Set pdfText = Nothing
           Set pdfBool = Nothing
           Set jso = Nothing
    End If

    aApp.Exit
    Set aApp = Nothing
    Unload userForm1

End Sub

CodePudding user response:

Ok, so after some searching, I have found a solution. Basically, forms created using Living Cycle don't work well with checkboxes. I asked somebody in my organization and they confirmed that Living Cycle was used on forms for a while until we got rid of it. Honestly, I don't know what Living Cycle is, but the solution seemed to work and so I think whatever the issue was related to something called "Living Cycle".

The solution? Redo the pdf form: I exported the pdf to an Encapsulated PostScript file. This stripped away all the fields. After that, I used the prepare form tool which automatically found all the relevant fields. Fortunately, with my pdf, it found all of the fields perfectly, though there was one or two extra ones that I had to delete. The field names and the code need to match so adjustments need to either be made to the PDF or to the code, but once I made that adjustment, everything was perfect.

  • Related