Need help with VBA Code

Cynthia Schadler 0 Reputation points
2023-06-20T21:39:50.4966667+00:00

I have used VBA Code to autofill a pdf form, it is working great. I have one issue though; the county is a drop down and when the vba code gets to that field it does not populate correctly. Please see snapshot below. When it gets to this field, it pulls my county from my excel spreadsheet, for example the county is Perquimans, but it keeps skipping to the next letter in the county name until the time on the code runs out like 5 seconds. It goes to the P's then the e's, etc. Help please what is the VBA Code to get this right?

User's image

Microsoft 365 and Office Excel For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Tanay Prasad 2,250 Reputation points
    2023-06-21T07:27:24.8533333+00:00

    Hi,

    Please modify your code as per the code below-

    Sub PopulateCountyDropdown()
        Dim AcroApp As Object
        Dim AcroAVDoc As Object
        Dim AcroForm As Object
        Dim CountyField As Object
        
        ' Create an instance of Acrobat application
        Set AcroApp = CreateObject("AcroExch.App")
        
        ' Open the PDF file
        Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
        If AcroAVDoc.Open("C:\Path\to\your\file.pdf", "") Then
            Set AcroForm = AcroAVDoc.GetPDDoc.GetForm
            
            ' Get the county field by its name
            Set CountyField = AcroForm.Fields("CountyFieldName")
            
            ' Set the value of the county field
            CountyField.Value = "Perquimans"
            
            ' Save and close the PDF file
            AcroAVDoc.Save 1, "C:\Path\to\save\output.pdf"
            AcroAVDoc.Close True
            
            ' Release the objects
            Set AcroForm = Nothing
            Set AcroAVDoc = Nothing
            Set AcroApp = Nothing
        End If
    End Sub
    

    Replace "CountyFieldName" with the actual field name of the county drop-down field in your PDF form.

    By using the Value property, you can directly set the desired value for the drop-down field without experiencing the issue you described.

    Best Regards.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.