Email mit Anhang versenden ueber outlook
abu
Hallo Zusammen,
habe folgenden Code gefunden und wuerde den gerne fuer meine Beduerfnisse umstricken. Leider bekomme ich immer eine Fehlermeldung bei dieser Zeile:
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
Wuerde mich ueber hilfe sehr freuen.
Hier noch kurz was es am Ende werden soll: Aus einer Userform heraus soll per Butten ein paar Daten aus den Textboxen in ein Arbeitsblatt kopiert werden (Vorlage is in der Arbeitsmappe vorhanden) Diese Vorlage soll dann in eine neue Arbeitsmappe kopiert werden diese soll dann einen bestimmten namen bekommen und an eine Email als anhang begefuegt werden und an eine bestimmte Adresse verschickt werden. Danach alles loeschen.
Hier der Code der funktioniert, leider nicht bei mir...
Private Sub CommandButton1_Click()
'Working in 2000-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 OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the sheets to a new workbook
Sourcewb.Sheets(Array("regel 2")).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
'grindlines weghalen
Application.CutCopyMode = False
ActiveWindow.DisplayGridlines = False
'Change all cells in the worksheets to values if you want
For Each sh In Destwb.Worksheets
sh.Select
With sh.UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Destwb.Worksheets(1).Select
Next sh
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Pick up inst" & " " & Range("C16") & " at " & Range("C19")
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = ThisWorkbook.Sheets("invoeren gegevens").Range("R2").Value
.CC = ""
.BCC = ""
.Subject = "Pick up instructions DHL Kyocera for order" & " " & Range("C16") & " at " _
& Range("C19")
.Body = "Hello," & vbCrLf & vbCrLf & _
"Please find attached our pick up order(s)." & vbCrLf & vbCrLf & _
"Complete and return the requested information as soon as possible, however due to _
security reasons at least 2 hours before collection." & vbCrLf & vbCrLf & _
"The truck driver has to be aware of number and has to provide this number at our _
premises." & vbCrLf & vbCrLf & _
"All information can be returned." & vbCrLf & vbCrLf & _
"If you have any questions, please do not hesitate to contact us." & vbCrLf & _
vbCrLf & _
"Many thanks in advance." & vbCrLf & vbCrLf & _
"Kind regards," & vbCrLf & vbCrLf & vbCrLf & _
"Operations Support Group" & vbCrLf & vbCrLf & _
"c" & vbCrLf & _
"c" & vbCrLf & _
"d" & vbCrLf & vbCrLf & _
"df" & vbCrLf & _
"Email: as" & vbCrLf & _
"Internet: d"
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
.ReadReceiptRequested = True
End With
On Error GoTo 0
.Close SaveChanges:=False
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub