【发布时间】: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 retrievethisarray(LBound(thisarray))` 或任何你怀疑出错的东西...... -
我这样做了,代码确实进入了循环,但由于某种原因,即使我有正确的文件路径,它也不会打开文件。 @FaneDuru