Sub SUK_Rechnung_Speichern_Und_Versenden()
On Error GoTo ErrorHandler ' Fehlerbehandlung aktivieren
Dim ws As Worksheet
Dim savePath As String
Dim fileName As String
Dim outlookApp As Object
Dim outlookMail As Object
Dim emailRecipient As String
Dim emailSubject As String
Dim emailBody As String
Dim subjectCell As Range ' Variable für Betreffzeile
Dim attachmentPath As String ' Pfad zu den Anhängen
Dim command As String ' Befehlszeile für PDFtk
Dim pdftkPath As String ' Pfad zum PDFtk-Server
' Arbeitsblatt mit den Daten
Set ws = ThisWorkbook.Sheets("SUK Rechnung")
' Pfad zum Speichern des PDFs
savePath = "W:\"
' Dateiname aus Zelle K11 auslesen
fileName = ws.Range("K11").Value
' PDF speichern
ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=savePath & "\" & fileName & ".pdf", Quality:=xlQualityStandard
' Betreffzeile auslesen
Set subjectCell = ws.Range("K12") ' Annahme: Betreff ist in Zelle K12
emailSubject = subjectCell.Value ' Wert der Betreffzeile zuweisen
' Outlook-Instanz erstellen
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
' Empfänger der E-Mail
emailRecipient = "michael.dedes@sanha.com"
' Text der E-Mail
emailBody = "Hallo Michael," & vbCrLf & _
vbCrLf & _
"anbei die Rechnungen." & vbCrLf & _
vbCrLf & _
"Viele Grüße," & vbCrLf & _
"Heike" & vbCrLf & _
"Accountant" & vbCrLf & _
"T +49 2054 925132" & vbCrLf & _
vbCrLf & _
"mailto:heike.ausderfuenten@sanha.com"
' Pfad zu den Anhängen
attachmentPath = "W:\Anhang WB\"
' Pfad zum PDFtk-Server
pdftkPath = """C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"""
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " """ & savePath & "\" & fileName & ".pdf"" "
' Alle PDF-Dateien im Anhangordner durchlaufen und zur Befehlszeile hinzufügen
Dim attachmentFileName As String
attachmentFileName = Dir(attachmentPath & "*.pdf") ' Alle PDF-Dateien im Ordner abrufen
Do While attachmentFileName <> "" ' Solange es Dateien gibt
command = command & """" & attachmentPath & attachmentFileName & """ "
attachmentFileName = Dir ' Nächste Datei abrufen
Loop
MsgBox "Befehlszeile: " & command
' Befehl zum Zusammenführen der PDFs hinzufügen
command = command & "cat output """ & savePath & "\" & fileName & "_with_attachments.pdf"""
' PDFs zusammenführen
Shell command, vbHide
' E-Mail zusammenstellen
With outlookMail
.To = emailRecipient
.Subject = emailSubject
.Body = emailBody
' Anhang hinzufügen (die kombinierte PDF-Datei)
.Attachments.Add savePath & "\" & fileName & "_with_attachments.pdf"
.Display ' E-Mail anzeigen
End With
Exit Sub ' Bei erfolgreicher Ausführung den Fehlerhandler überspringen
ub SUK_Rechnung_Speichern_Und_Versenden_Herbers()
On Error GoTo ErrorHandler ' Fehlerbehandlung aktivieren
Dim ws As Worksheet
Dim savePath As String
Dim fileName As String
Dim outlookApp As Object
Dim outlookMail As Object
Dim emailRecipient As String
Dim emailSubject As String
Dim emailBody As String
Dim subjectCell As Range ' Variable für Betreffzeile
Dim attachmentPath As String ' Pfad zu den Anhängen
Dim command As String ' Befehlszeile für PDFtk
Dim pdftkPath As String ' Pfad zum PDFtk-Server
' Arbeitsblatt mit den Daten
Set ws = ThisWorkbook.Sheets("SUK Rechnung")
' Pfad zum Speichern des PDFs
savePath = "W:" '####ohne \ am Ende
' Dateiname aus Zelle K11 auslesen
fileName = ws.Range("K11").Value
' PDF speichern
ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=savePath & "\" & fileName & ".pdf", Quality:=xlQualityStandard
' Betreffzeile auslesen
Set subjectCell = ws.Range("K12") ' Annahme: Betreff ist in Zelle K12
emailSubject = subjectCell.Value ' Wert der Betreffzeile zuweisen
' Outlook-Instanz erstellen
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
' Empfänger der E-Mail
emailRecipient = "michael.dedes@sanha.com"
' Text der E-Mail
emailBody = "Hallo Michael," & vbCrLf & _
vbCrLf & _
"anbei die Rechnungen." & vbCrLf & _
vbCrLf & _
"Viele Grüße," & vbCrLf & _
"Heike" & vbCrLf & _
"Accountant" & vbCrLf & _
"T +49 2054 925132" & vbCrLf & _
vbCrLf & _
"mailto:heike.ausderfuenten@sanha.com"
' Pfad zu den Anhängen
attachmentPath = "W:Anhang WB\" '##### mit \ am Ende
' Pfad zum PDFtk-Server
pdftkPath = """C:Program Files (x86)\PDFtk Server\bin\pdftk.exe"
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " " & savePath & "\" & fileName & ".pdf"
' Alle PDF-Dateien im Anhangordner durchlaufen und zur Befehlszeile hinzufügen
Dim attachmentFileName As String
attachmentFileName = Dir(attachmentPath & "*.pdf") ' Alle PDF-Dateien im Ordner abrufen
Do While attachmentFileName <> "" ' Solange es Dateien gibt
command = command & " " & attachmentPath & attachmentFileName
attachmentFileName = Dir ' Nächste Datei abrufen
Loop
MsgBox "Befehlszeile: " & command
' Befehl zum Zusammenführen der PDFs hinzufügen
command = command & " cat output " & savePath & "\" & fileName & "_with_attachments.pdf"""
' PDFs zusammenführen
Shell command, vbHide
' E-Mail zusammenstellen
With outlookMail
.To = emailRecipient
.Subject = emailSubject
.Body = emailBody
' Anhang hinzufügen (die kombinierte PDF-Datei)
.Attachments.Add savePath & "\" & fileName & "_with_attachments.pdf"
.Display ' E-Mail anzeigen
End With
Exit Sub ' Bei erfolgreicher Ausführung den Fehlerhandler überspringen
ErrorHandler:
End Sub
Sub test()
Dim pdftkPath As String, command As String, iReturnCode As Integer
Dim objWsh As Object
Set objWsh = VBA.CreateObject("WScript.Shell")
' Pfad zum PDFtk-Server
pdftkPath = """C:Program Files (x86)\PDFtk Server\bin\pdftk.exe"""
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " W:\*.pdf W:\catoutput_vba.pdf"
iReturnCode = objWsh.Run(command, vbHide, True)
MsgBox iReturnCode
End Sub
Sub test3()
Dim pdftkPath As String, command As String, iReturnCode As Integer
' Pfad zum PDFtk-Server
pdftkPath = """C:Program Files (x86)\PDFtk Server\bin\pdftk.exe"""
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " W:\*.pdf W:\catoutput_vba.pdf"
Shell command, vbHide
MsgBox "Fertig"
End Sub
Sub test()
Dim pdftkPath As String, command As String, iReturnCode As Integer
Dim objWsh As Object
Set objWsh = VBA.CreateObject("WScript.Shell")
' Pfad zum PDFtk-Server
pdftkPath = """C:Program Files (x86)\PDFtk Server\bin\pdftk.exe"""
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " W:\*.pdf cat output W:\catoutput_vba.pdf"
iReturnCode = objWsh.Run(command, vbHide, True)
MsgBox iReturnCode
End Sub
Sub SUK_Rechnung_Speichern_Und_Versenden()
On Error GoTo ErrorHandler ' Fehlerbehandlung aktivieren
Dim ws As Worksheet
Dim savePath As String
Dim fileName As String
Dim outlookApp As Object
Dim outlookMail As Object
Dim emailRecipient As String
Dim emailSubject As String
Dim emailBody As String
Dim subjectCell As Range ' Variable für Betreffzeile
Dim attachmentPath As String ' Pfad zu den Anhängen
Dim command As String ' Befehlszeile für PDFtk
Dim pdftkPath As String ' Pfad zum PDFtk-Server
' Arbeitsblatt mit den Daten
Set ws = ThisWorkbook.Sheets("SUK Rechnung")
' Pfad zum Speichern des PDFs
savePath = "W:" '####ohne \ am Ende
' Dateiname aus Zelle K11 auslesen
fileName = ws.Range("K11").Value
' PDF speichern
ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=savePath & "\" & fileName & ".pdf", Quality:=xlQualityStandard
' Betreffzeile auslesen
Set subjectCell = ws.Range("K12") ' Annahme: Betreff ist in Zelle K12
emailSubject = subjectCell.Value ' Wert der Betreffzeile zuweisen
' Outlook-Instanz erstellen
Set outlookApp = CreateObject("Outlook.Application")
Set outlookMail = outlookApp.CreateItem(0)
' Empfänger der E-Mail
emailRecipient = "michael.dedes@sanha.com"
' Text der E-Mail
emailBody = "Hallo Michael," & vbCrLf & _
vbCrLf & _
"anbei die Rechnungen." & vbCrLf & _
vbCrLf & _
"Viele Grüße," & vbCrLf & _
"Heike" & vbCrLf & _
"Accountant" & vbCrLf & _
"T +49 2054 925132" & vbCrLf & _
vbCrLf & _
"mailto:heike.ausderfuenten@sanha.com"
' Pfad zu den Anhängen
attachmentPath = "W:\Anhang WB\" '##### mit \ am Ende
' Pfad zum PDFtk-Server
pdftkPath = """C:\Program Files (x86)\PDFtk Server\bin\pdftk.exe"
' Befehlszeile für PDFtk
command = "cmd /c " & pdftkPath & " " & savePath & "\" & fileName & ".pdf"
' Alle PDF-Dateien im Anhangordner durchlaufen und zur Befehlszeile hinzufügen
Dim attachmentFileName As String
attachmentFileName = Dir(attachmentPath & "*.pdf") ' Alle PDF-Dateien im Ordner abrufen
Do While attachmentFileName <> "" ' Solange es Dateien gibt
command = command & " " & attachmentPath & attachmentFileName
attachmentFileName = Dir ' Nächste Datei abrufen
Loop
MsgBox "Befehlszeile: " & command
' Befehl zum Zusammenführen der PDFs hinzufügen
command = command & " cat output " & savePath & "\" & fileName & "_with_attachments.pdf"""
' PDFs zusammenführen
Shell command, vbHide
' E-Mail zusammenstellen
With outlookMail
.To = emailRecipient
.Subject = emailSubject
.Body = emailBody
' Anhang hinzufügen (die kombinierte PDF-Datei)
.Attachments.Add savePath & "\" & fileName & "_with_attachments.pdf"
.Display ' E-Mail anzeigen
End With
Exit Sub ' Bei erfolgreicher Ausführung den Fehlerhandler überspringen
ErrorHandler:
End Sub