Ik gebruik onderstaande macro om een bestand per mail naar mijn mail te sturen als Exel format.
Ik wil het echter als een .pdf naar mijn mail sturen.
Het probleem is echter dat het bestand iedere keer een ander naam heeft waarmee het word opgeslagen.
Hoe krijg ik het voor elkaar om het in een mail te krijgen als pdf zoals het onderstaande macro ook met een excel format gaat.
Ik wil het echter als een .pdf naar mijn mail sturen.
Het probleem is echter dat het bestand iedere keer een ander naam heeft waarmee het word opgeslagen.
Hoe krijg ik het voor elkaar om het in een mail te krijgen als pdf zoals het onderstaande macro ook met een excel format gaat.
Code:
Sub Send_Mail()
Application.DisplayAlerts = False
'Working in 97-2007
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the ActiveSheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
'We exit the sub when your answer is NO in the security dialog that you only
'see when you copy a sheet from a xlsm file with macro's disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
'TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
' TempFileName = " " & " " & Format(Now, "dd-mmm-yy h.mm uur")
TempFileName = ActiveSheet.Name & " " & Format(Now, "dd-mmm-yy h.mm uur")
With Destwb
' .Sheets(1).Shapes("CommandButton1").Delete
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close savechanges:=False
End With
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.xxx.nl"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
With iMsg
Set .Configuration = iConf
.To = "test@test.nl"
.CC = ""
.BCC = ""
.FROM = "test@test.nl"
.Subject = "" & ActiveSheet.Range("BB2").Text & ActiveSheet.Range("BB3").Text & " voor " & ActiveSheet.Range("BB4").Text & ""
.TextBody = "Beste " & vbCrLf & vbCrLf & _
"Hierbij " & ActiveSheet.Range("BB2").Text & ActiveSheet.Range("BB3").Text & " voor " & ActiveSheet.Range("BB4").Text & ""
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
CreateObject("WScript.Shell").Popup " De Factuur is nu ook naar je eigen Mail gestuurd !!", 2, " Ik sluit vanzelf na 2 Seconden"
End Sub