0

I have a working VBA script that pulls specific form fields from a specified PDF file into a spreadsheet. However I have several hundred PDFs that I need to do this for, so I'd like to loop through files in a directory and perform the same action.

Conveniently I have an old VBA script that loops through Word files in a directory and imports the content of each just how I'd like.

I hardly know VBA but I've adapted scripts in several language including VBA to meet my needs. I thought this would take 10 minutes but its taken several hours. Can somebody please look at my script below and tell me where I'm going wrong? I assume it has something to do with the Word and Acrobat libraries having different requirements, but even my loop isn't displaying the test message.

PS I have Acrobat Pro installed.

My Script (Non-Working)

Private Sub CommandButton1_Click()
    Dim f As String: f = "C:\temp\ocopy"
    Dim s As String: s = Dir(f & "*.pdf")
    Dim AcroApp As Acrobat.CAcroApp
    Dim theForm As Acrobat.CAcroPDDoc
    Dim jso As Object
    Dim text1, text2 As String
    Dim col As Integer: col = 1

    Do Until s = ""
        Set AcroApp = CreateObject("AcroExch.App")
        Set theForm = CreateObject("AcroExch.PDDoc")

        theForm.Open (f & s)
        Set jso = theForm.GetJSObject

        text1 = jso.getField("Name of serviceRow1").Value
        text2 = jso.getField("Who are the key contacts?").Value
        MsgBox text1
        MsgBox "text1"
        Sheet1.Cells(col, 1).Value = text1
        Sheet1.Cells(col, 2).Value = text2
        col = col + 1: s = Dir
        theForm.Close

        AcroApp.Exit
        Set AcroApp = Nothing
        Set theForm = Nothing
    Loop
End Sub

Word Script - Works at Looping and Importing

Sub fromWordDocsToMultiCols()
    Dim f As String: f = "C:\temp\Test\"
    Dim s As String: s = Dir(f & "*.docx")
    Dim wdApp As New Word.Application, wdDoc As Word.Document
    Dim col As Integer: col = 1

    On Error GoTo errHandler
    Do Until s = ""
        Set wdDoc = wdApp.Documents.Open(f & s)
        wdDoc.Range.Copy
        Sheet1.Cells(1, col).Value = s
        Sheet1.Cells(2, col).PasteSpecial xlPasteValues
        wdDoc.Close False: col = col + 1: s = Dir
    Loop

errHandler:
    If Err.Number <> 0 Then MsgBox Err.Description
    If Not wdApp Is Nothing Then wdApp.Quit False
End Sub

Acrobat Script - Works as Importing One-by-One

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\ocopy\Minerals asset management.pdf")
    Set jso = theForm.GetJSObject

    ' get the information from the form fiels Text1 and Text2
    text1 = jso.getField("Name of serviceRow1").Value
    text2 = jso.getField("Who are the key contacts within the team for this service? Please provide one contact per region").Value



    Sheet1.Cells(1, 1).Value = text1
    Sheet1.Cells(1, 2).Value = text2
    theForm.Close

    AcroApp.Exit
    Set AcroApp = Nothing
    Set theForm = Nothing


End Sub

Many thanks in advance.

Community
  • 1
  • 1
rylaughlin
  • 43
  • 8
  • The backslash is missing in the end here `"C:\temp\ocopy"` and therefore `s = Dir(f & "*.pdf")` fails, because there is no backslash between the path and the filename. It must be ` `"C:\temp\ocopy\"` – Pᴇʜ Feb 28 '18 at 13:54
  • Can you upload sample of a PDF file? – YasserKhalil Feb 28 '18 at 13:54
  • I am so dumb. That's exactly what was wrong. Thank you! – rylaughlin Feb 28 '18 at 14:03
  • 1
    @rylaughlin such things happen to everyone. But next time just debug your code by going through step-by-step (eg with F8) and check the values of your variables in every step. Then you will see such effects immediately on your own, and it saves you time asking a long question. **Just another thing:** `Dim text1, text2 As String` defines `text1` as variant and `text2` as string. You need to specify a type for every variable: `Dim text1 As String, text2 As String` to make them both of type `String`. – Pᴇʜ Feb 28 '18 at 14:25

0 Answers0