【问题标题】:Adobe Acrobat Pro DC is not responding to VBA commandsAdobe Acrobat Pro DC 不响应 VBA 命令
【发布时间】:2020-08-25 12:31:31
【问题描述】:

我目前正在编写一个从 Excel 索引中获取数字的宏。然后找到同名的PDF文件,合并成1个PDF文件。

但我无法让组合文件的部分正常工作,而且我无法找出它为什么不能正常工作。我的参考设置正确。

这是我遇到问题的部分。

Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim i As Integer
Dim iFailed As Integer
Dim strSaveAs As String
Dim MergePDFs As Boolean


strSaveAs = GetNewFolder & "\" & TxtNewFileName.Text
 
On Error GoTo NoAcrobat:
'Initialize the Acrobat objects
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
 
'Open Destination, all other documents will be added to this and saved with
'a New Filename
objCAcroPDDocDestination.Open (thisarray(LBound(thisarray))) 'open the first file
 
'Open each subsequent PDF that you want to add to the original
  'Open the source document that will be added to the destination
    For i = LBound(thisarray) + 1 To UBound(thisarray)
        
       If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
          MergePDFs = True
        Else
          'failed to merge one of the PDFs
          iFailed = iFailed + 1
        End If
        objCAcroPDDocSource.Close
    Next i
objCAcroPDDocDestination.Save 1, strSaveAs 'Save it as a new name
objCAcroPDDocDestination.Close
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing
 
NoAcrobat:
If iFailed <> 0 Then
    MergePDFs = False
End If
On Error GoTo 0

我希望这是足够的信息。我真的不想发布整个代码,因为它很长。 感谢您的努力。

【问题讨论】:

  • 当你尝试调试 On Error GoTo NoAcrobat 是不合适的。请评论它,并逐行运行代码(在 VBE 中按 F8),看看会发生什么。代码是否进入循环?放一些 Debug.Print' to retrieve thisarray(LBound(thisarray))` 或任何你怀疑出错的东西......
  • 我这样做了,代码确实进入了循环,但由于某种原因,即使我有正确的文件路径,它也不会打开文件。 @FaneDuru

标签: excel vba adobe acrobat


【解决方案1】:

请添加此行

objCAcroPDDocSource.Open (thisarray(i))

之后

For i = LBound(thisarray) + 1 To UBound(thisarray)

你没有打开Source文件,它没有被合并...

【讨论】:

  • 我已经添加了这一行,Adobe Acrobat Pro DC 只是没有响应,也不会出错。
  • @Tom:那么,您必须检查您的thisarray 数组是否包含多个元素,或者它们的全名不正确。我复制了您的代码并填写了thisarray打开必要的文件后效果很好。请尝试按原样复制您的简化代码,创建thisarray = Array(ThisWorkbook.Path &amp; "\" &amp; "someFile.pdf", ThisWorkbook.Path &amp; "\" &amp; "someFile2.pdf") 并运行代码。我发现问题正在处理,因为我建议您尝试调试...
  • 当我尝试这种方法时它可以工作,但是这种方法的输出与我自动填充数组时的输出相同。但是,当我自动执行此操作时,它不起作用。我按照你的建议做了,在数组之前使用 F8 和断点来看看会发生什么,但这对我来说并不奇怪。 @FaneDuru
  • @Tom:这个不容置疑的证明你的数组元素是错误的......上面是一个工作代码!。您必须看到文件全名的确切外观。按照我的建议尝试:Debug.Print thisarray(i): Stop。看看它会返回什么。不需要F8。代码将停在这一行......也试试,Debug.Print thisarray(LBound(thisarray)):Stop 在循环上方。
  • 非常感谢您的帮助。我发现了这个问题。它与文件名而不是代码有关。它试图找到的文件不存在,这就是它不起作用的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
相关资源
最近更新 更多