【问题标题】:Script to delete Outlook messages is not deleting all messages删除 Outlook 邮件的脚本不会删除所有邮件
【发布时间】:2016-10-21 15:59:13
【问题描述】:

我正在尝试根据电子邮件标题中的特定标签删除 Outlook 中的邮件。具体来说,如果标题中多次出现“X-ZANTAZ-RECIP”,我想保留该消息。如果它只在标题中一次,我想删除该消息。这是我正在进行的存档项目的一部分。

我在 vba 和 powershell 中有脚本。两者似乎都有效,但我必须运行它 5 次,然后才能删除所有仅出现一次此标头标记的消息。我正在处理的样本集包含约 70,000 条消息。第一遍删除约 24,000 个。第二遍〜11,000。第三遍~3000...

关于为什么这不会在第一遍删除所有适用消息的任何想法?

Powershell:

$outlook = New-Object -ComObject 'Outlook.Application'
$currentFolder = ($outlook.ActiveExplorer()).CurrentFolder.Items
Foreach ($objemail in $currentFolder){
    $objheader = $objemail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
    $objoccurances = ([regex]::Matches($objheader, "X-ZANTAZ-RECIP" )).count
    If ($objoccurances -lt 2){
        $objemail.Delete()
        }
    Write-Host $objoccurances
    }

VBA:

Sub DeleteMessages()
    Dim olItem As Outlook.MailItem, olMsg As Outlook.MailItem
    Dim strheader As String
    Dim output As String
    Dim CountOccurrences As Long

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

    Next

    Set olMsg = Nothing
    MsgBox "finished"
End Sub

Function GetInetHeaders(olkMsg As Outlook.MailItem) As String
    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)
    CountOccurrences = UBound(Split(GetInetHeaders, "X-ZANTAZ-RECIP"))
    If CountOccurrences < 2 Then
        olkMsg.Delete
    End If
    Set olkPA = Nothing
End Function

【问题讨论】:

    标签: vba powershell email outlook


    【解决方案1】:

    不要在改变项目数量的循环中删除项目。使用向下循环(VB 中的for i = Items.Count to 1 step -1)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2018-01-06
      • 2021-03-12
      • 2019-07-29
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多