gpt4 book ai didi

excel - 循环不打开 PDF...所以复制/粘贴到 Excel 失败(我认为)

转载 作者:行者123 更新时间:2023-12-02 02:57:51 25 4
gpt4 key购买 nike

提前为这篇文章的篇幅道歉,但我想详细描述我的问题,希望你们中的一位 VBA 大师能提供帮助

目标

  1. 遍历文件夹中的所有 PDF
  2. 对于每个 PDF:全选/复制/粘贴到 Excel 中
  3. 调用一个单独的宏将粘贴的数据转换成一些东西清晰可辨。

背景下面的子 [CopyPDFtoExcel()] 昨天工作,但现在在带有

的 ActiveSheet.Paste 行上失败

"Runtime error '1004' Paste method of Worksheet class failed".

如果我单步执行(通过 F8),它似乎实际上并未打开 PDF,因此无法选择全部/复制/粘贴,从而产生运行时错误。但是,我没有收到错误对话框,如果它找不到文件,我想我会收到(来自 Debug.Assert False)。

我的 fName 通过名为 path2008 的命名范围定义为变量。这些文件路径是通过运行 PullFilePathsforPDFs() 派生的,它会为我的文件夹中的每个 PDF 吐出完整的文件路径。然后,我选择了这些文件路径并为其命名,在本例中为 path2008,它用于 13 个不同的 PDF。 注意:此文件夹中实际上有 250 多个 PDF,但我选择了一个子集进行测试,因此有 13 个与 path2008 相关联。

到目前为止我做了什么

  1. 使用 path2008 范围内的每个 PDF 测试了文件路径(繁琐的非循环)ActiveWorkbook.FollowHyperlink 方法,成功打开所有 PDF。所以,我很有信心文件路径正确。

    'ActiveWorkbook.FollowHyperlink "file path here" 
  2. 删除了全选/复制/粘贴 VBA 代码,只留下循环 [参见子 TroubleshootingOpeningPDFLoop()]。当我踏上通过第一次黄线从 Set oPDDoc =oAVDoc.GetPDDoc 行到 End If.... 大概意味着它找到了一个在第一个循环期间的文件(虽然我没有看到打开的 PDF)。在第二个(以及所有后续循环)它转到 Else 然后Debug.Assert False(但没有出现错误对话框)。

  3. 重新启动 Excel 和 Acrobat,同样的问题

  4. 重启电脑,同样的问题

  5. 重新创建了一个新的工作簿,同样的问题

主要代码

Sub CopyPDFtoExcel()
Dim fName As Variant
Dim wbPayroll As Excel.Workbook
Dim wsConvert As Excel.Worksheet
Dim oPDFApp As AcroApp
Dim oAVDoc As AcroAVDoc
Dim oPDDoc As AcroPDDoc

Set wbPayroll = Workbooks("Payroll.xlsm")
Set wsConvert= wbPayroll.Sheets("Convert")

Set oPDFApp = CreateObject("AcroExch.App")
Set oAVDoc = CreateObject("AcroExch.AVDoc")
Set oPDDoc = CreateObject("AcroExch.PDDoc")

'Open the PDF file. The AcroAVDoc.Open function returns a true/false
For Each fName In Range("path2008")
If oAVDoc.Open(fName.Text, "") = True Then
Set oPDDoc = oAVDoc.GetPDDoc
Else
Debug.Assert False
End If

'Copy all using Acrobat menu
oPDFApp.MenuItemExecute ("SelectAll")
oPDFApp.MenuItemExecute ("Copy")

'Paste into Convert sheet
wbPayroll.Activate
wsConvert.Cells(1, 1).Select
ActiveSheet.Paste 'It worked yesterday, but now error on this line with below error
'Runtime error '1004' Paste method of Worksheet class failed

oAVDoc.Close (1) '(1)=Do not save changes
'oPDDoc.Close
Call ConversionMacro
Next

'Clean up
Set wbTransfer = Nothing
Set wsNew = Nothing
Set oPDFApp = Nothing
Set oAVDoc = Nothing
Set oPDDoc = Nothing
End Sub

我为隔离 PDF 打开失败问题所做的努力

Sub TroubleshootingOpeningPDFLoop()
Dim fName As Variant
Dim wbPayroll As Excel.Workbook
Dim wsConvert As Excel.Worksheet
Dim oPDFApp As AcroApp
Dim oAVDoc As AcroAVDoc
Dim oPDDoc As AcroPDDoc

'Define your spreadsheet
Set wbPayroll = Workbooks("Payroll.xlsm")
Set wsConvert= wbPayroll.Sheets("Convert")

'Instantiate Acrobat Objects
Set oPDFApp = CreateObject("AcroExch.App")
Set oAVDoc = CreateObject("AcroExch.AVDoc")
Set oPDDoc = CreateObject("AcroExch.PDDoc")

'Open the PDF file. The AcroAVDoc.Open function returns a true/false
For Each fName In Range("path2008")
If oAVDoc.Open(fName.Text, "") = True Then
Set oPDDoc = oAVDoc.GetPDDoc
Else
Debug.Assert False
End If
Next
End Sub

Sub 用于拉取文件路径

Sub PullFilePathsforPDFs()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:\Stuff\MoreStuff") 'all PDFs I need are stored here
i = 1
For Each objFile In objFolder.Files
Cells(i + 1, 1) = objFile.Name
Cells(i + 1, 2) = objFile.Path
i = i + 1
Next objFile
End Sub

最佳答案

我最近遇到了剪切/复制-粘贴问题(运行时错误 1004)使用最新的 Excel,但也在处理“古老”的应用程序。对我来说诀窍是使用原始的“给定”名称(Sheet1、Sheet2 等。一旦我添加/重命名相同的工作表,运行时错误又回来了。

关于excel - 循环不打开 PDF...所以复制/粘贴到 Excel 失败(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com