VBA abrufen HTMLBody von Outlook-E-mail
Zuerst erstelle ich eine E-Mail über Outlook:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)
Dim sHTML_Open As String
Dim sHTML_Introduction As String
Dim sHTML_Goodbye As String
Dim sHTML_Close As String
Dim sHTML_Process_Date As String
Dim sHTML_Processor As String
Dim sHTML_Issuer As String
Dim sHTML_Details As String
Dim sHTML_Body As String
sHTML_Open = "<HTML><BODY>"
sHTML_Introduction = "Hi team,<BR/><BR/>" & _
"Data is ready to process. Please find details as below.<BR/>"
sHTML_Process_Date = "<P ID='PROCESSDATE'>28 February 2013</P>"
sHTML_Processor = "<P ID='PROCESSOR'>AKSHAY</ID></P>"
sHTML_Issuer = "<P ID='ISSUER'>DATAGROUP.COM</ID></P>"
sHTML_Details = "<P ID='DETAILS'>" & _
"<UL>" & _
"<LI>Fimta23456 09:00:00 flor345</LI>" & _
"<LI>Fimta23456 09:00:00 flor345</LI>" & _
"</UL>" & _
"</P><BR/>"
sHTML_Goodbye = "Thanks"
sHTML_Close = "</BODY></HTML>"
sHTML_Body = sHTML_Open & sHTML_Introduction & sHTML_Process_Date & sHTML_Processor & sHTML_Issuer & _
sHTML_Details & sHTML_Goodbye & sHTML_Close
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.To = "Kim Gysen"
.Subject = "data remit file"
.HTMLBody = sHTML_Body
.Display
End With
End Sub
Per code, ich möchte Werte abzurufen, basierend auf ID.
Dies schien der sauberste Weg für mich, ich don ' T besonders wie die "split" - Methode, weil es ist eine Art von hard-Codierung; nicht sehr dynamische und irgendwie unzuverlässig.
Leider, wenn ich das abrufen der HTML-Körper, die ich nicht abrufen kann, der original-HTML, wie es ist verzerrt durch Outlook:
Sub Get_OL()
Dim oFolder As MAPIFolder
Dim oItem As Variant
Dim sHTML_Body As String
Dim sHTML_Process_Date As String
Dim sHTML_Processor As String
Dim sHTML_Issuer As String
Dim sHTML_Details As String
Dim oExcel As Object
Dim oBook As Workbook
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
'Access the outlook inbox folder
Set oFolder = GetNamespace("MAPI").PickFolder
'On error resume next usually not to use, but feteching emails may give unexpected errors
On Error Resume Next
For Each oItem In oFolder.Items
If TypeOf oItem Is Outlook.MailItem Then
If oItem.Subject Like "*data remit file*" Then
'Turn off on error resume next asap
On Error GoTo 0
sHTML_Body = oItem.HTMLBody
Debug.Print sHTML_Body
Exit For
End If
End If
Next oItem
End Sub
Auf debug.drucken, das ist, was ich bekomme (nur indem die Letzte Zeile des Formats):
</o:shapelayout></xml><![endif]--></head><body lang=EN-GB link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi team,<br><br>Data is ready to process. Please find details as below.<br><br><o:p></o:p></p><p>28 February 2013<o:p></o:p></p><p id=PROCESSOR>AKSHAY<o:p></o:p></p><p id=ISSUER>DATAGROUP.COM<o:p></o:p></p><ul type=disc><li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'>Fimta23456 09:00:00 flor345<o:p></o:p></li><li class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo1'>Fimta23456 09:00:00 flor345<o:p></o:p></li></ul><p class=MsoNormal><br>Thanks<o:p></o:p></p></div></body></html>
Ich möchte zum abrufen der ursprünglichen HTML-Code, ich habe in den HTMLBody.
- Wo ist diese E-Mail-wechseln zwischen
CreateHTMLMail
und Wann suchen Sie es? Gibt es Schritte gibt, die fehlen aus dem code oben. - Sorry für die späte Antwort. Ich erstelle die mail mit CreateHTMLMail, ich schickte es an mich, dann rufe ich es mit Get_OL. Ich würde gerne abrufen der gleiche HTML-Code wie ich es ursprünglich erstellt wurde.
- Der Grund warum ich Frage ist, testete ich fast deinen genauen code und bin in der Lage, zum abrufen des HTML-genau wie Sie erzeugt wurden. Sind Sie auf exchange? Oder mit so etwas wie gmail, etc?
- Ich benutze Outlook. Kann es etwas damit zu tun haben mit RTF-Einstellungen?
Du musst angemeldet sein, um einen Kommentar abzugeben.
2 Möglichkeiten:
1) Parsen von text - mehrere Dinge zu tun (nicht empfohlen: hard-coding)
Alles, was Sie brauchen, ist Parsen von text, aber MSDN zeigt, wie man es InStr Funktion. Ich würde dringend empfehlen RegEx zum Parsen von html-text. Hinweis: Verweis auf MS VBScript Regular Expressions X. x benötigt wird.
Einfache Reguläre Ausdrücke Tutorial für Excel-VBA
2) mit UserProperites des MailItem-Objekts (empfohlen)
Wenn MailItem nicht enthält, die die Eigenschaft(y)ies, als es nichts zu tun 😉
Gewusst wie: Hinzufügen der benutzerdefinierten Eigenschaft