我正在研究一个宏,以使用户可以在使用主物料清单的情况下构建零件的PDF书。除了最后一部分,我已经弄清楚了所有内容,最后一部分是为要合并的PDF位置构建一串文件路径。由于PDF的数量可能不同,所以我不能使用固定长度的字符串。但是,当我切换到可变长度字符串时,会遇到一些错误。 所有文件路径(如果存在)将使用SQL连接在A列的Excel文档中。如果PDF不存在,则可能会有空格。空白导致宏失败。 我的问题是1-我可以使用可变长度的字符串来实现这一点吗?和2-如何解决空白导致宏失败的问题? 请参阅下面的当前代码-问题出在Sub Combine()部分。先感谢您。
Private Function MergePDFs(arrFiles() As String, strSaveAs As String) As Boolean
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim i As Integer
Dim iFailed As Integer
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 (arrFiles(LBound(arrFiles))) '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(arrFiles) + 1 To UBound(arrFiles)
objCAcroPDDocSource.Open (arrFiles(i))
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
End Function
Sub Combine()
Dim y As Integer
y = Application.WorksheetFunction.CountA(Columns(1))
'determine last cell with value to include in string
Dim strPDFs(1 To 3) As String
Dim bSuccess As Boolean
Dim i As Integer
For i = 1 To y
If Sheets("Sheet1").Cells(i, 1) = "" Then i = i + 1 Else strPDFs(i) = Sheets("Sheet1").Cells(i, 1)
Next i
'checks for blanks, if blank skips, if not blank adds cell value to string
bSuccess = MergePDFs(strPDFs, "C:\Users\EVance\Desktop\TEST.pdf")
If bSuccess = False Then MsgBox "Failed to combine all PDFs", vbCritical, "Failed to Merge PDFs"
'merges PDFs into one file named TEST
End Sub