Sub PrintNaarPDF()
Dim UserInput As String, SheetArray As Variant, FilePath As String
Dim i As Long
Dim antwoord As VbMsgBoxResult
Dim ws As Worksheet
Dim ExistingSheets As New Collection
Dim choice As VbMsgBoxResult
On Error Resume Next
For Each ws In Worksheets
ExistingSheets.Add ws.Name, ws.Name
Next ws
On Error GoTo 0
choice = MsgBox("Wil je de eerste 5 tabbladen printen (Ja)" & vbCrLf & "of zelf tabbladen kiezen (Nee)", vbQuestion + vbYesNo, "Keuze")
If choice = vbYes Then
ReDim SheetArray(1 To 5)
For i = 1 To 5
If i <= Worksheets.Count Then
SheetArray(i) = Worksheets(i).Name
Else
MsgBox "Er zijn minder dan 5 tabbladen in deze werkmap.", vbExclamation
Exit Sub
End If
Next i
Else
UserInput = InputBox("Voer de namen van de tabbladen in die je wilt printen, gescheiden door een komma:")
If Trim(UserInput) = "" Then
MsgBox "Let op: Geen tabbladen opgegeven.", vbExclamation
Exit Sub
End If
SheetArray = Split(UserInput, ",")
For i = LBound(SheetArray) To UBound(SheetArray)
SheetArray(i) = Trim(SheetArray(i))
Next i
End If
For i = LBound(SheetArray) To UBound(SheetArray)
On Error Resume Next
Dim temp As String
temp = ExistingSheets(SheetArray(i))
If Err.Number <> 0 Then
MsgBox "Tabblad '" & SheetArray(i) & "' bestaat niet.", vbExclamation
Exit Sub
End If
On Error GoTo 0
Next i
FilePath = Application.GetSaveAsFilename(FileFilter:="PDF-bestanden (*.pdf), *.pdf", Title:="Opslaan als PDF")
If FilePath = "False" Or FilePath = "" Then
MsgBox "Geen bestandsnaam opgegeven. Printopdracht geannuleerd.", vbExclamation
Exit Sub
End If
If LCase(Right(FilePath, 4)) <> ".pdf" Then
FilePath = FilePath & ".pdf"
End If
Worksheets(SheetArray).Copy
With ActiveWorkbook
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FilePath
.Close False
End With
antwoord = MsgBox("Printopdracht voltooid. Wil je het PDF-bestand nu openen?", vbQuestion + vbYesNo, "Print voltooid")
If antwoord = vbYes Then
Shell "cmd /c start """" """ & FilePath & """", vbHide
End If
End Sub