2013-04-22 183 views
2

首先我通過Outlook創建電子郵件:VBA檢索HTMLBody從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 

通過代碼,我想要檢索基於ID值。 這對我來說似乎是最乾淨的方式,我不特別喜歡「分割」方法,因爲它是一種硬編碼;不是很有活力,有點不可靠。

不幸的是,當我取回HTML的身體,我無法找回原來的HTML,因爲它是由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 

在debug.print,這是我得到什麼(只把最後一行格式):

</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> 

我想檢索我放入HTMLBody的原始HTML。

+0

這封電子郵件在'CreateHTMLMail'和你正在看的時候在哪裏?上面的代碼中缺少那些步驟。 – enderland 2013-04-26 20:54:06

+0

對不起,延遲迴復。我使用CreateHTMLMail創建郵件,然後將它發送給我自己,然後使用Get_OL檢索它。我希望檢索與我最初創建相同的HTML。 – Trace 2013-04-29 12:49:18

+0

我問的原因是我測試了幾乎你的確切代碼,並能夠完全按照創建的方式檢索HTML。你在交換嗎?或者使用類似gmail等的東西? – enderland 2013-04-29 13:09:17

回答

1

2種方式:

1)解析文本 - 幾件事情要做(不建議:硬編碼)

所有你需要的是parse text,但MSDN展示瞭如何做它使用InStr函數。我強烈建議使用RegEx解析html文本。注意:參考需要MS VBScript正則表達式x.x

Simple Regular Expression Tutorial for Excel VBA

2)使用UserProperites的MailItem對象(推薦

如果的MailItem不包含您PROPERT(Y)IES,比沒有什麼做的;)

How to: Add custom property

+0

感謝您的回答,雖然我不再處理這個問題。儘管如此,也許你的答案對其他人有幫助。 – Trace 2014-01-06 13:19:17

相關問題