Ich habe zahlreiche Antworten gelesen, die nah an dem sind, was ich suche, aber jedes Mal funktioniert es nicht in meinem Code.Attach erstellt Word-Dokument zu Outlook-Nachricht
Das sollte eine ziemlich grundlegende Frage sein, aber ich hoffe, dass jemand sich das ansehen und meinen Fehler schnell sehen kann.
Ich verwende Excel, um ein Word Doc zu erstellen, das dann auf dem Desktop des Benutzers in einem Ordner mit dem aktuellen Datum gespeichert wird.
Alles funktioniert perfekt, aber jetzt alles, was ich versuche zu tun, ist, den Word-Dokument den Namen der Zeichenfolge "IRN", die eine Zelle in das Excel-Arbeitsblatt ist.
Ich muss auch das erstellte Worddokument an eine Outlook-Nachricht anhängen.
Ich werde nur die Einleitung und das Ende meines Codes enthalten, da der Körper keine Rolle spielen sollte.
Sub TDOutlook()
Dim TD As Word.Application
Dim Doc As Word.Document
Dim path As String
Dim filename As String
Dim StudentName As String
Dim StudentAddress1 As String
Dim City As String
Dim MrMrs As String
Dim StudentLast As String
Dim IRN As String
Dim CourseReq As String
Dim CourseName As String
Dim CourseStart As String
Dim Cost As String
Dim Deferred As String
Dim Graphic As String
Dim Footer1 As Word.Range
Dim Body As Word.Paragraph
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
path = Environ("USERPROFILE") & "\Desktop\" & Format(Now, "mm-dd-yyyy")
On Error Resume Next
MkDir path
On Error GoTo 0
'Outlook
Set OutApp = CreateObject("Outlook.Application")
Set objOutlookMsg = OutApp.CreateItem(olMailItem)
objOutlookMsg.SentOnBehalfOfName = "[email protected]"
objOutlookMsg.Subject = "FinServ-TD"
objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
'Display Outlook
objOutlookMsg.Display
'Opens Word
Set TD = CreateObject("Word.Application")
'Displays the document
TD.Visible = False
'Add New Document
Set Doc = TD.Documents.Add
filename = path & "\TD" '& IRN
Doc.SaveAs filename
'Attach Word to Outlook
objOutlookMsg.Attachments.Add Doc.filename <----This is broken
Doc.Close
TD.Quit
'Application.ScreenUpdating = True
End Sub
Try filename = Pfad & "TD" & Sheets ("Sheet1"). Range ("A1") .Text - angenommen, das IRN ist in Zelle A1 – 0m3r
Siehe So speichern Sie es mit Zelle Wert http://StackOverflow.com/a/30393989/4539709 – 0m3r
Vielen Dank Om3r, ich habe es nicht in meinem Text aber die Variable enthalten IRN wurde auf diese spezifische Zelle im sh gesetzt Eet aber es würde es aus irgendeinem Grund nicht in den Dateinamen einschließen, aber die direkte Verbindung mit der Zelle, wie Sie angegeben haben, funktionierte perfekt. Wissen Sie, wie ich das Wort doc als Anhang zu meiner offenen Outlook-E-Mail zufügen kann? –