如何使用VBA脚本从outlook 2007+中的标题中提取“for”/“to”地址

时间:2015-10-16 13:50:22

标签: vba outlook outlook-addin outlook-vba

我正在使用BlueDevilFan VBA脚本向我展示Outlook 2013中的电子邮件的互联网标题。我一直在尝试修改VBA,以便它只输出'to'或'for'地址并丢弃所有其他文本,

以下是VBA脚本:

 Sub ViewInternetHeader()
    Dim olItem As Outlook.MailItem, olMsg As Outlook.MailItem
    Dim strheader As String

    For Each olItem In Application.ActiveExplorer.Selection
        strheader = GetInetHeaders(olItem)

        Set olMsg = Application.CreateItem(olMailItem)
        With olMsg
            .BodyFormat = olFormatPlain
            .Body = strheader
            .Display
        End With
    Next
    Set olMsg = Nothing
End Sub

Function GetInetHeaders(olkMsg As Outlook.MailItem) As String
    ' Purpose: Returns the internet headers of a message.'
    ' Written: 4/28/2009'
    ' Author:  BlueDevilFan'
    ' http://techniclee.wordpress.com/
    ' Outlook: 2007'
    Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
    Dim olkPA As Outlook.PropertyAccessor
    Set olkPA = olkMsg.PropertyAccessor
    GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
    Set olkPA = Nothing
End Function

我尝试了类似于我会在单词中查找和删除某些内容的内容,但无法在Outlook中使用它!

Sub CleanUp()
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "(\To: """)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Text = "(\To: """)"
.Execute Replace:=wdReplaceAll
End With
End With
End Sub

2 个答案:

答案 0 :(得分:0)

你可以清除身体的文本,然后添加来自发件人

x

答案 1 :(得分:0)

为什么需要从标题中提取地址?只需遍历MailItem.Recipients集合中的所有收件人,如果Recipient.Type = olTo,请阅读Recipient.Address属性/