Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Dim sbody As String
Set xSht = Worksheets("sjabloon")
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
With xFileDlg
.InitialFileName = Sheets("blad1").Range("b2")
End With
If xFileDlg.Show = True Then
xFolder = xFileDlg.SelectedItems(1)
Else
MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
Exit Sub
End If
xFolder = xFolder + "\" & xSht.Cells(20, 4) & " " & xSht.Cells(23, 9) & ".pdf"
'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
xYesorNo = MsgBox(xFolder & "Bestaat al." & vbCrLf & vbCrLf & "Wilt u deze overschrijven?", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorNo = vbYes Then
Kill xFolder
Else
MsgBox "niet opgeslagen." _
& vbCrLf & vbCrLf & "Druk OK om af te sluiten", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number <> 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If
Set xUsedRng = xSht.UsedRange
Dim xaccount As Object
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
'Create Outlook email
Dim i As Integer
'Shell ("OUTLOOK") 'deze mag dus weg blijkbaar
Dim Handtekening As String
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.createitem(0)
Set xaccount = xOutlookObj.Session.accounts.Item(1)
Set asht = Worksheets("blad2")
Dim bestand As String
bestand = xSht.Cells(1, 3)
With xEmailObj
Set .Sendusingaccount = xaccount
.display
Handtekening = .HTMLBody
.To = xSht.Cells(12, 3)
.CC = xSht.Cells(11, 3)
.HTMLBody = ****verwijderd voor privacy****
.Subject = xSht.Cells(20, 4) & " " & xSht.Cells(23, 9)
.Attachments.Add xFolder
If Not Dir(bestand) = vbNullString Then
.Attachments.Add bestand
Else
MsgBox ("Kan de locatie: " & bestand & " niet vinden!")
End If
If DisplayEmail = False Then
End If
End With
Else
MsgBox "niet blanco"
Exit Sub
End If
End Sub