我正在编写代码来创建一份包含多个部分的报告到一个 PDF 中。这需要一个封面页并插入一个章节页和报告,然后移动到下一个章节页和报告。该代码在循环中直到最后一个文档之前都运行良好。无论数组中有多少项,它都永远不会添加最后一个文档。它添加了最后一个部分页面,并且最后一个报告不会进入 PDF。如果有(1x)SectionArray 文档和(1x)PathArray 文档,则将插入SectionArray,而不会插入PathArray 文档。如果有 5 个文档,则所有 4 个文档都将与主 PDF 合并,而最后一个则不会。我尝试过不同的 PDF 以检查是否存在安全或权限问题。这似乎是循环中最后一个文档的问题。循环和 Adobe SDK 是否有问题?
不知道该怎么办。请参阅代码的修改部分,了解我正在做的事情的示例:
'Cover page location that was already
finalFilePath = "FINAL DOCUMENT PATH\Project Name Close-Out Package.pdf"
'Section page file locations and reports to combine into the final PDF
'SectionArray will always be the same length as the PathArray
Dim SectionArray() As String
SectionArray(0) = "SectionPage/Path/Here"
SectionArray(1) = "SectionPage/Path/Here"
SectionArray(2) = "SectionPage/Path/Here"
Dim PathArray() As String
PathArray(0) = "Document/Path/Here"
PathArray(1) = "Document/Path/Here"
PathArray(2) = "Document/Path/Here"
Dim AcroApp As Acrobat.CAcroApp
Set AcroApp = CreateObject("AcroExch.App")
Set primaryDoc = CreateObject("AcroExch.PDDoc")
ok = primaryDoc.Open(finalFilePath)
Dim xcounter As Integer
xcounter = 0
For Each L In PathArray
numPages = primaryDoc.GetNumPages() - 1
Debug.Print "numPages: " & numPages
Set sectiondoc = CreateObject("AcroExch.PDDoc")
If sectiondoc.Open(SectionArray(xcounter)) Then
numberOfPagesToInsert = sectiondoc.GetNumPages()
ok = primaryDoc.InsertPages(numPages, sectiondoc, 0, numberOfPagesToInsert, False)
Else
Debug.Print "Could not open file: " & SectionArray(xcounter)
End If
sectiondoc.Close
Set sectiondoc = Nothing
numPages2 = primaryDoc.GetNumPages() - 1
Debug.Print "numPages2: " & numPages2
Set sourceDoc = CreateObject("AcroExch.PDDoc")
If sourceDoc.Open(PathArray(xcounter)) Then
Debug.Print PathArray(xcounter)
Debug.Print "Open Document Succeeded"
numberOfPagesToInsert2 = sourceDoc.GetNumPages()
Debug.Print numberOfPagesToInsert2
If primaryDoc.InsertPages(numPages2, sourceDoc, 0, numberOfPagesToInsert2, False) Then
Debug.Print "Inserted Pages: " & ok
Else
Debug.Print "Could not add pages: " & PathArray(xcounter)
End If
Else
Debug.Print "Could not open file: " & PathArray(xcounter)
End If
xcounter = xcounter + 1
Next L
ok = primaryDoc.Save(PDSaveFull, finalFilePath)
primaryDoc.Close
Set primaryDoc = Nothing
AcroApp.Exit
Set AcroApp = Nothing
您可以重新排列代码以删除一些重复的部分(未经测试):
Sub ConstData()
'Cover page location that was already
Const finalFilePath As String = "FINAL DOCUMENT PATH\Project Name Close-Out Package.pdf"
Const PDSaveFull = 1
Dim sources, i As Long, pth As String, numPages As Long
Dim primaryDoc As Object, sourceDoc As Object, numberOfPagesToInsert As Long
'array of alternating pairs of section + document
sources = Array("SectionPage/Path/Here1", "Document/Path/Here1", _
"SectionPage/Path/Here2", "Document/Path/Here2", _
"SectionPage/Path/Here3", "Document/Path/Here3")
Set primaryDoc = CreateObject("AcroExch.PDDoc")
If Not primaryDoc.Open(finalFilePath) Then
Debug.Print "### Could not open primary doc!"
Exit Sub
End If
For i = LBound(sources) To UBound(sources)
numPages = primaryDoc.GetNumPages() - 1
Set sourceDoc = CreateObject("AcroExch.PDDoc")
pth = sources(i)
If sourceDoc.Open(pth) Then
Debug.Print "Inserting from '" & pth & "'"
numberOfPagesToInsert = sourceDoc.GetNumPages()
If primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False) Then
Debug.Print , "Inserted " & numberOfPagesToInsert & " pages"
Else
Debug.Print , "### Failed insert of " & numberOfPagesToInsert & " pages"
Exit Sub
End If
sourceDoc.Close 'close only if the document could be opened...
Else
Debug.Print , "### Could not open file!"
Exit Sub
End If
Next i
If primaryDoc.Save(PDSaveFull, finalFilePath) Then
primaryDoc.Close
Debug.Print "---------Process Complete-------------"
Else
Debug.Print "### Failed to save compiled PDF!"
End If
End Sub