0

我有一个有效的 VBA 脚本,可以将指定 PDF 文件中的特定表单字段提取到电子表格中。但是,我需要为此执行数百个 PDF,因此我想遍历目录中的文件并执行相同的操作。

方便的是,我有一个旧的 VBA 脚本,它循环遍历目录中的 Word 文件,并按照我的喜好导入每个文件的内容。

我几乎不了解 VBA,但我已经用包括 VBA 在内的多种语言改编了脚本以满足我的需求。我以为这需要 10 分钟,但它需要几个小时。有人可以看看我下面的脚本并告诉我哪里出错了吗?我认为这与具有不同要求的 Word 和 Acrobat 库有关,但即使我的循环也没有显示测试消息。

PS 我已经安装了 Acrobat Pro。

我的脚本(非工作)

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 脚本 - 用于循环和导入

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 脚本 - 用作一对一导入

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

提前谢谢了。

4

0 回答 0