将多个PDF转换为Excel - 无法启动APP?

时间:2015-11-04 15:15:14

标签: excel vba excel-vba pdf adobe

我有几千个类似的PDF文件,我想将其转换为Excel工作簿。

我对VBA并不是很熟悉,甚至不太熟悉Adobe API,但是我从这个website得到了这个代码,这似乎正是我需要的。

但是,每次我尝试运行代码(我通过单击转换文件下载了第二个版本btw,它是一个zip文件夹),我的Excel立即崩溃,弹出框为" Microsoft Excel已停止工作......"

所以我决定查看代码以检查它到底打嗝的确切位置。

  1. 首先,我按照指示,进入工具 - >引用并检查Adobe Acrobat 10.0类型库。我安装了Adobe Acrobat Pro DC,并且卸载了所有以前版本的Adobe Reader。我使用Excel 2010,所以我不一定认为有任何兼容性问题......

  2. 代码分为两部分。外部部分是完全正确的 - 它检查你是否有文件路径以及是否所有内容都是正确的格式,然后调用sub" SavePDFAs"。通过随机添加MsgBox" tests",我可以告诉代码完全正确地通过这部分。

  3. Sub SavePDFAs中出现错误,其中代码通过创建APP对象来初始化Acrobat。看起来Excel无法创建APP对象......有人可以帮我调试吗?此时,由于Excel崩溃,我甚至不确定从哪里开始寻找解决方案......

  4. 我已附上以下代码。

       Private Sub SavePDFAs(PDFPath As String, FileExtension As String)
        'Saves a PDF file as other format using Adobe Professional.
    
        'In order to use the macro you must enable the Acrobat library from VBA editor:
        'Go to Tools -> References -> Adobe Acrobat xx.0 Type Library, where xx depends
        'on your Acrobat Professional version (i.e. 9.0 or 10.0) you have installed to your PC.
    
        'Alternatively you can find it Tools -> References -> Browse and check for the path
        'C:\Program Files\Adobe\Acrobat xx.0\Acrobat\acrobat.tlb
        'where xx is your Acrobat version (i.e. 9.0 or 10.0 etc.).
    
        'By Christos Samaras
        'Date: 30/03/2013
        'http://www.myengineeringworld.net
        '---------------------------------------------------------------------------------------
    
        Dim objAcroApp      As Acrobat.AcroApp
        Dim objAcroAVDoc    As Acrobat.AcroAVDoc
        Dim objAcroPDDoc    As Acrobat.AcroPDDoc
        Dim objJSO          As Object
        Dim boResult        As Boolean
        Dim ExportFormat    As String
        Dim NewFilePath     As String
    
        'Initialize Acrobat by creating App object.
        Set objAcroApp = CreateObject("AcroExch.App")
    
    
        'Set AVDoc object.
        Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")
    
        'Open the PDF file.
        boResult = objAcroAVDoc.Open(PDFPath, "")
    
        'Set the PDDoc object.
        Set objAcroPDDoc = objAcroAVDoc.GetPDDoc
    
        'Set the JS Object - Java Script Object.
        Set objJSO = objAcroPDDoc.GetJSObject
    
        'Check the type of conversion.
        Select Case LCase(FileExtension)
            Case "eps": ExportFormat = "com.adobe.acrobat.eps"
            Case "html", "htm": ExportFormat = "com.adobe.acrobat.html"
            Case "jpeg", "jpg", "jpe": ExportFormat = "com.adobe.acrobat.jpeg"
            Case "jpf", "jpx", "jp2", "j2k", "j2c", "jpc": ExportFormat = "com.adobe.acrobat.jp2k"
            Case "docx": ExportFormat = "com.adobe.acrobat.docx"
            Case "doc": ExportFormat = "com.adobe.acrobat.doc"
            Case "png": ExportFormat = "com.adobe.acrobat.png"
            Case "ps": ExportFormat = "com.adobe.acrobat.ps"
            Case "rft": ExportFormat = "com.adobe.acrobat.rft"
            Case "xlsx": ExportFormat = "com.adobe.acrobat.xlsx"
            Case "xls": ExportFormat = "com.adobe.acrobat.spreadsheet"
            Case "txt": ExportFormat = "com.adobe.acrobat.accesstext"
            Case "tiff", "tif": ExportFormat = "com.adobe.acrobat.tiff"
            Case "xml": ExportFormat = "com.adobe.acrobat.xml-1-00"
            Case Else: ExportFormat = "Wrong Input"
        End Select
    
        'Check if the format is correct and there are no errors.
        If ExportFormat <> "Wrong Input" And Err.Number = 0 Then
    
            'Format is correct and no errors.
    
            'Set the path of the new file. Note that Adobe instead of xls uses xml files.
            'That's why here the xls extension changes to xml.
            If LCase(FileExtension) <> "xls" Then
                NewFilePath = WorksheetFunction.Substitute(PDFPath, ".pdf", "." & LCase(FileExtension))
            Else
                NewFilePath = WorksheetFunction.Substitute(PDFPath, ".pdf", ".xml")
            End If
    
            'Save PDF file to the new format.
            boResult = objJSO.SaveAs(NewFilePath, ExportFormat)
    
            'Close the PDF file without saving the changes.
            boResult = objAcroAVDoc.Close(True)
    
            'Close the Acrobat application.
            boResult = objAcroApp.Exit
    
        Else
    
            'Something went wrong, so close the PDF file and the application.
    
            'Close the PDF file without saving the changes.
            boResult = objAcroAVDoc.Close(True)
    
            'Close the Acrobat application.
            boResult = objAcroApp.Exit
    
        End If
    
        'Release the objects.
        Set objAcroPDDoc = Nothing
        Set objAcroAVDoc = Nothing
        Set objAcroApp = Nothing
    
    End Sub
    

    ---- ----- EDIT 有人指出代码实际上是有效的,而且只是我的Excel发生了故障。有关我需要为Excel更改哪些设置有什么问题的想法?

    ---编辑2 --- 我打电话给服务台,我得到的答案是,每个Adobe版本都有一些非常怪异的怪癖与以前的版本不兼容。我的同事的Adobe许可证是10,而我的是DC,因此它不能在我的机器上工作。不一定是我想听到的答案......

3 个答案:

答案 0 :(得分:1)

Set objAcroApp = CreateObject("AcroExch.App") 报错的原因可能是:

您只能在整个脚本中创建一次 Adob​​e App,这意味着您不能在循环中使用此 Sub SavePDFAs(),因为每次迭代都会在其中创建 App。

尝试将包含所有 PDF 路径(您要转换的路径)的数组传递到此 Sub 中,并且:

  1. 只设置一次 objAcroApp = CreateObject("AcroExch.App");
  2. 循环 boResult = objJSO.SaveAs(NewFilePath, ExportFormat) 以转换所有 PDF。

答案 1 :(得分:0)

我遇到了类似的问题,经过研究后发现了一个在Adobe上进行安装修复的建议。之后我的代码运行正常。不知道为什么!

答案 2 :(得分:0)

尝试删除以下代码,我有类似的问题,我让MS Excel决定昏暗。

Dim objAcroApp      As Acrobat.AcroApp
Dim objAcroAVDoc    As Acrobat.AcroAVDoc
Dim objAcroPDDoc    As Acrobat.AcroPDDoc