从特定文本字段导出 PDF 值并将其导入 Xls 文件中的特定单元格中

问题描述 投票:0回答:1

我有以下宏,但它不起作用。 我已经安装了 Adobe Acrobat Pro。 一方面,我担心我没有拥有所需的所有参考资料(如果知道所需的参考资料列表以及如何获取它们,这将是一个很大的帮助。例如,我没有 Adobe Acrobat 10.0类型库。)。

目前,我遇到了错误:“对象不支持属性或方法”,引用行 SHIPSNAME = pdfDoc.GetField("SHIPSNAME CREWLIST").Value。

请让我走上正确的道路来解决这个问题。这对我的工作非常重要

Sub ImportaDaPDF()

    ' Imposta il percorso del file PDF
    Dim pdfPath As String
    pdfPath = "C:\Users\@@@@\Desktop\Document.pdf"  ' Sostituisci con il percorso del tuo file PDF
    
    ' Crea un oggetto Acrobat
    Dim acrobatApp As Object
    Set acrobatApp = CreateObject("AcroExch.App")

    ' Crea un oggetto PDF
    Dim pdfDoc As Object
    Set pdfDoc = CreateObject("AcroExch.PDDoc")

    ' Apri il file PDF
    If pdfDoc.Open(pdfPath) Then

        ' Estrai i valori dai campi testo
        Dim SHIPSNAME As String
        SHIPSNAME = pdfDoc.GetField("SHIPSNAME CREWLIST").Value
        
        Dim FLAGSTATE As String
        FLAGSTATE = pdfDoc.GetField("FLAGSTATE CREWLIST").Value

        ' Chiudi il file PDF
        pdfDoc.Close

        ' Chiudi l'applicazione Acrobat
        acrobatApp.Exit

        ' Inserisci i valori nelle celle specifiche di Excel
        Dim wsShip As Worksheet
        Set wsShip = ThisWorkbook.Sheets("Page 1")  ' Sostituisci con il nome del tuo foglio
        wsShip.Range("A4").Value = SHIPSNAME

        ' Inserisci i valori nelle celle specifiche di Excel
        Dim wsPortCall As Worksheet
        Set wsPortCall = ThisWorkbook.Sheets("Page 2")  ' Sostituisci con il nome del tuo foglio
        wsPortCall.Range("B4").Value = FLAGSTATE

    Else

    MsgBox "Errore durante l'apertura del file PDF. Dettagli: " & Err.Description

End If

End Sub

我需要从 PDF 文档的特定文本字段中提取值,并将这些值导入到 Xls 文件的特定单元格中。 我首先尝试了一个更长的过程,基本上我是将这些值作为文本发送到电子邮件正文中,然后将它们复制并粘贴到 CSV 文件中,但它不起作用,甚至一点也不起作用,因为相同的宏我用来从 CSV 文件导入数据在其他情况下不起作用

excel vba windows csv acrobat
1个回答
0
投票

这对我使用测试 PDF 表单很有用:

Private Sub ReadPdfFormFields()
    Dim jso As Object, text1, text2 As String
    
    With CreateObject("AcroExch.PDDoc")
        .Open "C:\temp\Test.pdf"        'load the file
        Set jso = .GetJSObject
        
        ' get the information from the form fields
        text1 = jso.getField("FieldNumber1").Value
        text2 = jso.getField("FieldNumber2").Value
    
        Debug.Print "Values read from PDF: " & vbLf & text1 & vbLf & text2
        .Close        'close the file
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.