首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换多个PDF到Excel -无法启动应用程序?

转换多个PDF到Excel -无法启动应用程序?
EN

Stack Overflow用户
提问于 2015-11-04 15:15:13
回答 3查看 1.8K关注 0票数 0

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

我不太熟悉VBA,更不熟悉Adobe,但我从这个网站中获得了这段代码,这似乎正是我所需要的。

然而,每次我试图运行代码(我下载了第二个版本,其中它是一个压缩文件夹),我的Excel立即崩溃与弹出框的“微软Excel已停止工作.”

因此,我决定查看代码,以检查它到底在哪里打嗝。

  1. 首先,我遵循了指南,进入了tools ->引用,并检查了AdobeAcrobat10.0类型库。我已经安装了Acrobat,并且卸载了Adobe的所有以前版本。我在使用Excel 2010,所以我不认为有任何兼容性问题.
  2. 代码分为两部分。外部部分是完全好的-它检查您是否有文件路径在那里和是否一切都是正确的格式,然后它调用子“SavePDFA”。通过添加随机的MsgBox“测试”,我可以看出代码完全可以很好地通过这个部分。
  3. 错误发生在Sub SavePDFA中,其中代码通过创建APP对象初始化Acrobat。似乎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,因此它不能在我的机器上工作。不一定是我想听到的答案..。

EN

回答 3

Stack Overflow用户

发布于 2021-04-16 07:53:06

在Set objAcroApp = CreateObject("AcroExch.App")处引发错误的原因可能是:

您只能在整个脚本中创建一次Adobe,这意味着不能在循环中使用这个SavePDFAs(),因为在每次迭代时都要在哪个应用程序中创建。

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

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

Stack Overflow用户

发布于 2015-12-03 16:56:10

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

票数 0
EN

Stack Overflow用户

发布于 2015-12-03 19:08:46

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

代码语言:javascript
复制
Dim objAcroApp      As Acrobat.AcroApp
Dim objAcroAVDoc    As Acrobat.AcroAVDoc
Dim objAcroPDDoc    As Acrobat.AcroPDDoc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33525366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档