I can't test this right now.
In the sheet's code:
Private Sub CommandButton1_Click() Dim i As Integer, j As Integer, s As String, a() As Variant
Dim pdfDirSource As String, pdfSource As String, pdfTarget As String
pdfDirSource = ThisWorkbook.Path & "\" 'Use trailig backslash
pdfTarget = "Drawings.pdf"
If Len(Dir(pdfDirSource, vbDirectory)) = 0 Then
MsgBox pdfDirSource & vbLf & vbLf & "Macro is ending.", vbCritical, "Path Does Not Exist"
Exit Sub
End If
j = 1
ReDim a(1 To 1)
a(1) = ""
For i = 1 To 13
With ActiveSheet
If .CheckBoxes("Check Box " & i).Value = 1 Then
s = .Shapes("Check Box " & i).AlternativeText & ".pdf"
pdfSource = pdfDirSource & s
If Dir(pdfSource) <> "" Then
ReDim Preserve a(1 To j)
a(j) = s
j = j + 1
End If
End If
End With
Next i
'Merge the pdfs
If a(1) <> "" Then
s = GetFolder("Merge Folder", CreateObject("WScript.Shell").SpecialFolders("Desktop"))
s = s & "Merged.pdf"
Debug.Print s
MergePDFs pdfDirSource, Join(a, ","), s
'Show the merged pdf
'Shell "cmd /c " & s
End If
End Sub
In a Module:
Function GetFolder(Optional sTitle As String = "Select Folder", _ Optional sInitialFilename As String)
Dim myFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
If sInitialFilename = "" Then sInitialFilename = ThisWorkbook.Path
.initialFilename = sInitialFilename
.Title = sTitle
If .Show = -1 Then
GetFolder = .SelectedItems(1)
If Right(GetFolder, 1) <> "\" Then
GetFolder = GetFolder & "\"
End If
Else: GetFolder = ""
End If
End With
End Function
Sub MergePDFs(MyPath As String, MyFiles As String, Optional DestFile As String = "MergedFile.pdf")
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: VBE - Tools - References - Acrobat
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
' Adjust MyPath string if needed.
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
' Save to MyPath folder if target folder for merged PDF file was not input. (ken)
If InStr(DestFile, "\") = 0 Then DestFile = p & DestFile
On Error GoTo exit_
If Len(Dir(DestFile)) Then Kill DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Dir(p & Trim(a(i))) = "" Then
MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
Exit For
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & Trim(a(i))
If i Then
' Merge PDF to PartDocs(0) document
ni = PartDocs(i).GetNumPages()
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
End If
' Calc the number of pages in the merged document
n = n + ni
' Release the memory
PartDocs(i).Close
Set PartDocs(i) = Nothing
Else
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & DestFile, vbExclamation, "Canceled"
End If
End If
exit_:
' Inform about error/success
If Err Then
MsgBox Err.Description, vbCritical, "Error #" & Err.Number
ElseIf i > UBound(a) Then
MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
End If
' Release the memory
If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
Set PartDocs(0) = Nothing
' Quit Acrobat application
AcroApp.Exit
Set AcroApp = Nothing
End Sub