我有几千个类似的PDF文件,我想要转换为Excel工作簿。
我不太熟悉VBA,更不熟悉Adobe,但我从这个网站中获得了这段代码,这似乎正是我所需要的。
然而,每次我试图运行代码(我下载了第二个版本,其中它是一个压缩文件夹),我的Excel立即崩溃与弹出框的“微软Excel已停止工作.”
因此,我决定查看代码,以检查它到底在哪里打嗝。
我已经把密码附在下面。
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
-编辑-有人指出,这段代码实际上是可以工作的,而只有我的Excel出现了故障。对于我需要为我的Excel修改哪些设置有什么问题,有什么想法吗?
--编辑2--我给服务台打了电话,我得到的答案是,每个Adobe版本都有一些与以前版本不兼容的奇怪之处。我的同事的Adobe许可证是10,而我的是DC,因此它不能在我的机器上工作。不一定是我想听到的答案..。
发布于 2021-04-16 07:53:06
在Set objAcroApp = CreateObject("AcroExch.App")处引发错误的原因可能是:
您只能在整个脚本中创建一次Adobe,这意味着不能在循环中使用这个SavePDFAs(),因为在每次迭代时都要在哪个应用程序中创建。
尝试将包含所有PDF路径的数组传递到此Sub中(您希望将这些路径转换),并且:
发布于 2015-12-03 16:56:10
我也遇到了类似的问题,在研究之后,我发现了一个在Adobe上进行安装修复的建议。在那之后,我的代码运行得很好。不知道为什么!
发布于 2015-12-03 19:08:46
尝试删除以下代码,我有类似的问题,我让MS Excel决定的Dim。
Dim objAcroApp As Acrobat.AcroApp
Dim objAcroAVDoc As Acrobat.AcroAVDoc
Dim objAcroPDDoc As Acrobat.AcroPDDochttps://stackoverflow.com/questions/33525366
复制相似问题