簡體   English   中英

How to extract "type" - Email or Web from an Outlook Email using VBA?

[英]How to extract "type" - Email or Web from an Outlook Email using VBA?

我嘗試使用 VBA 從 Outlook email 中提取數據:

Sub DLPExtract()
    
    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As MailItem
    Dim i As Integer
    
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("gfdo@aviva.com\Inbox")
    
    i = 1
    
    For Each OutlookMail In Folder.Items
        If InStr(OutlookMail.Subject, "Data Loss Prevention Report: GFDO DLP Daily Report Retrospective") > 0 And OutlookMail.ReceivedTime >= Range("From_date").Value Then
        
            Range("Date").Offset(i, 0).Value = OutlookMail.Date
            Range("Type").Offset(i, 0).Value = OutlookMail.
            Range("Reference").Offset(i, 0).Value = OutlookMail.ID
            Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body
            
            i = i + 1
        End If
    Next OutlookMail
    
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
    
End Sub

我只想提取第四列中的 ID,但首先需要檢查它是否已經在 Excel 表的相應列中。 如果是,則什么也不做,如果否,則從 email 的第四列中取出該 ID,並將其粘貼到 Excel 表的相應列中。

在此處輸入圖像描述

我相信您正在嘗試檢查 email 正文格式。

如果是這種情況,那么請點擊討論“Bodyformat”屬性的鏈接

Range("Type").Offset(i, 0).Value = OutlookMail.BodyFormat

目前尚不清楚您對什么“類型”屬性感興趣 - Outlook object model 不為其項目提供Type屬性。 相反,您可能對MessageClass屬性感興趣,該屬性返回或設置表示 Outlook 項目的消息 class 的字符串。 MessageClass屬性將項目鏈接到它所基於的表單。 選擇項目后,Outlook 使用消息 class 來定位表單並公開其屬性,例如回復命令。

此外,您可能對BodyFormat屬性感興趣,該屬性返回或設置指示正文格式的OlBodyFormat常量。 正文文本格式決定了用於顯示消息文本的標准。 Microsoft Outlook 提供三種正文文本格式選項:純文本、富文本 (RTF) 和 HTML。

最后,無需遍歷文件夾中的所有項目並檢查它們是否符合預定義的條件:

For Each OutlookMail In Folder.Items
    If InStr(OutlookMail.Subject, "Data Loss Prevention Report: GFDO DLP Daily Report Retrospective") > 0 And OutlookMail.ReceivedTime >= Range("From_date").Value Then

相反,我建議使用Items class 的Find / FindNextRestrict方法。 在以下文章中閱讀有關它們的更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM