【问题标题】:Outlook restrict method not finding some items in Inbox and/or sent boxOutlook 限制方法未在收件箱和/或已发送框中找到某些项目
【发布时间】:2022-01-02 02:56:10
【问题描述】:

您好,我有一个脚本可以循环遍历 Excel 表,以检查我是否收到了对已发送电子邮件的回复。该脚本在过去几个月中运行良好,但现在它在我的收件箱(outlook)中找不到(随机?)某些项目 我已经根据每封电子邮件中的 3 个标准构建了一个 DASL 过滤器。 我不确定这是否是引用我的 Outlook 文件夹的问题。在计算我的收件箱中受过滤器限制的项目时,items.count 返回 0,这是不正确的。 我知道我的 DASL 过滤器不是最好的,但我在字符串连接方面有点挣扎。我正在分享我可以成功找到的电子邮件的屏幕截图。用于查找该电子邮件的标准以红色突出显示。

screenshot of test email

Sub Search_InboxAndHighlight()

Dim myOlApp As New Outlook.Application
Dim objNamespace As Outlook.Namespace
'Dim objFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.Folder
Dim filteredItems As Outlook.Items
Dim Sup_ENg_Number As Range
Dim OGDD_Programs As Range
Dim strFilter As String
Dim compName As String
Dim supNumber As String
Dim Program_type as String

'set a reference to cells we are going to loop through
Set OGDD_Programs = ActiveSheet.Range("A2", "A" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible)


Set objNamespace = myOlApp.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set fol = objNamespace.Folders("myEmailaddress").Folders("Sent Items")


"loop through cells on activesheet

For Each Sup_ENg_Number In OGDD_Programs

  supNumber = "'" & Sup_ENg_Number.Value & "'"
   compName = Sup_ENg_Number.Offset(, 1).Value
   compName = "'" & compName & "'"
  Program_type = "'" & Sup_ENg_Number..Offset(, 3)Value & "'"

   ' filter to search sup_eng_number  and company name in body of the eemail
     strFilter = "@SQL=""urn:schemas:httpmail:textdescription"" ci_phrasematch " & supNumber
     strFilter = strFilter & " AND "
     strFilter = strFilter & """urn:schemas:httpmail:textdescription"" ci_phrasematch " & compName
     strFilter = strFilter & " AND "
     strFilter = strFilter & """urn:schemas:httpmail:textdescription"" ci_phrasematch " & Program_type
  
   'restrict items to lookup to be only of the filter "strFilter"
    Set filteredItems = objFolder.Items.Restrict(strFilter)
    Set SentFilteredItems = fol.Items.Restrict(strFilter)

    If filteredItems.Count = 0 AND SentFilteredItems.Count = 0 then

      Sup_ENg_Number.Interior.Color = vbRed
   end if
Next Sup_ENg_Number 

 MsgBox "Completed!"

'If the subject isn't found:
If Not Found Then
    'NoResults.Show
Else
   'Debug.Print "Found " & filteredItems.Count & " items."

End If
Application.ScreenUpdating = True
'myOlApp.Quit
Set myOlApp = Nothing

End Sub

【问题讨论】:

  • 发布应该与您的 DASL 过滤器匹配的电子邮件图像。 Program_type 未定义。
  • 我手动重写了上面的代码,是的,我在真实代码中定义了 Program_type,不幸的是我无法显示屏幕截图,因为它包含敏感信息..我昨天玩过并删除了 Program_Type 标准,filteredItems.Count返回一些东西,但我需要使用所有 3 个标准来找到正确的电子邮件
  • 创建一个仅用于测试的虚拟邮件并发布它的图像。我尝试使用 View->View Settings->Filter->Advanced->Field->Frequently-used-fields->Message 创建 DASL 过滤器以匹配您的过滤器,但我没有得到相同的过滤器!您是如何创建它们的?
  • 我按照脚本所示创建了它们,我将尝试创建一个虚拟电子邮件

标签: vba outlook


【解决方案1】:

如果您有多个收件箱,则默认收件箱可能不再是原来的。

而不是

Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

保持一致

Set objFolder = objNamespace.Folders("myEmailaddress").Folders("Inbox")

【讨论】:

  • 嗨,我也尝试了上述方法但没有成功,我的问题只发生在数百封电子邮件中的 7 封......而且它还在我的已发送邮件框中返回了错误的计数
  • 如果“已发送邮件”文件夹结果也受到影响,您可以编辑问题以删除“收件箱与已发送邮件”。考虑通过删除 Excel 并将数据硬编码到示例代码中以及添加不包含敏感信息的示例电子邮件文本来简化问题。
  • 我给自己发了一封测试邮件,没问题,我昨天可以找到一些邮件,但今天我的脚本找不到它们。
  • 我给自己发了一封测试邮件,没问题,我昨天可以找到一些邮件,但今天我的脚本找不到它们。我不相信脚本会找到一些电子邮件,并且 2 天后它就再也找不到它们了。我正在显示我的测试电子邮件的屏幕截图,用我的脚本找到它没问题
  • 编辑问题以添加最新信息。表示结果不一致。在那里包括测试电子邮件。为潜在的响应者提供足够的细节来证明/反驳代码中的问题。
【解决方案2】:

我重写了删除 Excel 的代码。

这个显然不错代码遵循https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-search-for-a-phrase-in-the-body-of-items-in-a-folder中的示例。

它会找到旧邮件,但找不到正文中带有“Test”的新测试电子邮件。
忘了我正在测试默认邮箱和辅助邮箱。代码有效。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Sub Search_Inbox()

    ' Code for Outlook not Excel
        
    Dim objFolder As folder
    Dim fol As folder
    
    Dim filteredItems As Items
    Dim SentFilteredItems As Items
    
    Dim strFilter As String
    Dim supNumber As String
    
    Dim i As Long
    
    'Default Inbox
    'Set objFolder = Session.GetDefaultFolder(olFolderInbox)
    'Set fol = Session.GetDefaultFolder(olFolderSentMail)

    'Any inbox
    Set objFolder = Session.folders("myEmailaddress").folders("Inbox")
    'Set fol = Session.folders("myEmailaddress").folders("Sent")
    Set fol = Session.folders("myEmailaddress").folders("Sent Items")

    'https://docs.microsoft.com/en-us/office/client-developer/outlook/pia/how-to-search-for-a-phrase-in-the-body-of-items-in-a-folder
    If (Session.DefaultStore.IsInstantSearchEnabled) Then
    
        ' compare this sample filter to your filter for supNumber
        strFilter = "@SQL=" & Chr(34) _
          & "urn:schemas:httpmail:textdescription" & Chr(34) _
          & " ci_phrasematch 'office'"
        Debug.Print strFilter
        
        supNumber = "Test"
        'Debug.Print supNumber
        supNumber = "'" & supNumber & "'"
        'Debug.Print supNumber
        
        ' filter to search for supNumber
        strFilter = "@SQL=""urn:schemas:httpmail:textdescription"" ci_phrasematch " & supNumber
        Debug.Print strFilter
        
        'restrict items
        Set filteredItems = objFolder.Items.Restrict(strFilter)
        Debug.Print "Found " & filteredItems.Count & " inbox items."
        
        Set SentFilteredItems = fol.Items.Restrict(strFilter)
        Debug.Print "Found " & SentFilteredItems.Count & " sent items."
        
        If filteredItems.Count = 0 And SentFilteredItems.Count = 0 Then
            Debug.Print "No items found."
        Else
            For i = 1 To filteredItems.Count
                Debug.Print filteredItems(i).subject
                Debug.Print filteredItems(i).ReceivedTime
                Debug.Print filteredItems(i).body
            Next
            
            For i = 1 To SentFilteredItems.Count
                Debug.Print SentFilteredItems(i).subject
                Debug.Print SentFilteredItems(i).ReceivedTime
                Debug.Print SentFilteredItems(i).body
            Next
            
        End If
        
        MsgBox "Completed!"
        
    Else
    
        MsgBox "Experiment with like. May be buggy as well."
        
    End If

End Sub

您在评论中说“我有一些电子邮件,我昨天可以找到,但今天我的脚本找不到它们。”

如果没有关于您看到的不可靠性的解释,您可以尝试使用 like 而不是 ci_phrasematch。您可以考虑使用.Find

【讨论】:

  • 谢谢我要试试你的代码,我也注意到了一些事情,我的代码设置了对我的离线收件箱的引用,与服务器存储的收件箱相比,它包含很少的电子邮件:2800 件与 48K 件
猜你喜欢
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2014-07-13
  • 2012-09-12
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多