结合PDF与VBA

我试图将一个PDF数组合并成一个这样的代码:

Option Explicit Sub Fusion_PDFs(ByVal name As String, ByRef pdfs() As Variant) Dim oPDDoc() As Object Dim oPDDocFinal As Object Dim Num As Long Dim i As Integer Set oPDDocFinal = CreateObject("AcroExch.PDDoc") oPDDocFinal.Open (pdfs(0)) ReDim oPDDoc(UBound(pdfs)) For i = LBound(pdfs) + 1 To UBound(pdfs) Set oPDDoc(i) = CreateObject("AcroExch.PDDoc") oPDDoc(i).Open (pdfs(i)) Next i For i = LBound(oPDDoc) To UBound(oPDDoc) Num = oPDDocFinal.GetNumPages() - 1 oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True Next i oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 'Application.DisplayAlerts = False For i = LBound(oPDDoc) To UBound(oPDDoc) oPDDoc(i).Close Set oPDDoc(i) = Nothing Next i oPDDocFinal.Close Set oPDDocFinal = Nothing 'Application.DisplayAlerts = True End Sub 

我从另一个包含pdf的Xpath的函数获得了一个string数组。 我已经validation了这个数组,并没有什么错,它是这个代码的问题。 但是我做了一个testing版本,然后将其重写为与我的项目一起工作,testing版本完美地工作。 代码仍然非常相似,我没有改变创build和融合部分。

我首先打开一个oPDDocFinal,这是我的数组“pdfs”(pdfs(0))的第一个pdf,然后我循环其余的pdf数组创build一个PDDoc数组。 最后,我循环这个PDDoc数组,将所有这些pdf与oPDDocFinal结合起来。

但是我在这条线上有一个错误:

 oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 

我得到了以下错误(我试图从法语翻译):

执行错误“91”:

对象variables或与集团variables未定义

我没有修改这部分代码,它正在我的testing脚本,但现在我得到这个错误。 你知道我怎么解决我的问题?

感谢您的关注。

好吧,我发现我的错误:

我的第一个循环,我从1开始,所以我把pdf(1)到oPDDoc(1),但我的第一个循环从0开始,所以oPDDoc(0)不存在。

我固定它,现在它工作:

 Option Explicit Sub Fusion_PDFs(ByVal name As String, ByRef pdfs() As Variant) Dim oPDDoc() As Object Dim oPDDocFinal As Object Dim Num As Long Dim i As Integer Set oPDDocFinal = CreateObject("AcroExch.PDDoc") oPDDocFinal.Open (pdfs(0)) ReDim oPDDoc(UBound(pdfs)) For i = LBound(pdfs) + 1 To UBound(pdfs) Set oPDDoc(i - 1) = CreateObject("AcroExch.PDDoc") oPDDoc(i - 1).Open (pdfs(i)) Next i For i = LBound(oPDDoc) To UBound(oPDDoc) - 1 Num = oPDDocFinal.GetNumPages() - 1 oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True Next i oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 'Application.DisplayAlerts = False 'For i = LBound(oPDDoc) To UBound(oPDDoc) - 1 ' ' oPDDoc(i).Close ' Set oPDDoc(i) = Nothing ' 'Next i ' 'oPDDocFinal.Close 'Set oPDDocFinal = Nothing 'Application.DisplayAlerts = True End Sub 

谢谢大家的关注!

试一试: –

  • 它所处的环境是否安装了相同版本的AcroExch和Word
  • 两种环境都可以看到PDF文件吗?
  • 是否有争议的oPDDocFinal意思是某人或其他人打开( 这个线程意味着它应该被closures更新)。
  • 在debugging中,oPDDoc(i)是否有一个值
  • oPDDocFinal.InsertPages(Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True)

我也相信你可能在一个单一的循环debugging更容易。

 Dim oPDDoc As Object Dim oPDDocFinal As Object Dim Num As Long Dim i As Integer 'Initialise objects Set oPDDocFinal = CreateObject("AcroExch.PDDoc") Set oPDDoc = CreateObject("AcroExch.PDDoc") 'Save a working copy oPDDocFinal.Open (pdfs(0)) oPDDocFinal.Save 1, ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" oPDDocFinal.Close 'Reference the working copy pdfs(0) = ThisWorkbook.Path & "\DRT créés\" & name & ".pdf" 'for all but the first item in the pdfs array For i = LBound(pdfs) + 1 To UBound(pdfs) 'Open the working copy oPDDocFinal.Open (pdfs(0)) 'Open the additional PDF oPDDoc.Open (pdfs(i)) 'Get the page count of the working copy Num = oPDDocFinal.GetNumPages() - 1 'Insert the additional PDF at the end of the working copy oPDDocFinal.InsertPages Num, oPDDoc(i), 0, oPDDoc(i).GetNumPages(), True 'Close the additional PDF oPDDoc.Close 'Save and close the working copy PDF oPDDocFinal.Save oPDDocFinal.Close Next i 'Release objects Set oPDDocFinal = Nothing Set oPDDoc = Nothing 

这将是一个重量级循环,但应作为debugging的起点。 我也应该补充我没有AcroExch 。 以上是理论上的。