【问题标题】:how to apply filter only on outlook messages using vba如何使用 vba 仅对 Outlook 消息应用过滤器
【发布时间】:2016-01-25 14:03:03
【问题描述】:

以下代码从 Outlook 中获取所有未分类的项目,但它返回所有项目,包括约会和会议。我需要一个代码,它只返回未分类的消息。

Sub NullCategoryRestriction() 
    Dim oFolder As Outlook.Folder 
    Dim oItems As Outlook.Items 
    Dim Filter As String 

    'DASL Filter can test for null property. 
    'This will return all items that have no category. 
    Filter = "@SQL=" & Chr(34) & _ 
     "urn:schemas-microsoft-com:office:office#Keywords" & _ 
     Chr(34) & " is null" 
    Set oFolder = Application.ActiveExplorer.CurrentFolder 
    Set oItems = oFolder.Items.Restrict(Filter) 
    Debug.Print oItems.Count 
End Sub

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您还需要将MessageClass 属性检查包括到过滤器中。该属性返回一个字符串,表示 Outlook 项目的邮件类别。

    【讨论】:

      【解决方案2】:

      以下代码对我有用。

      Filter = "@SQL=" & Chr(34) & _
           "urn:schemas-microsoft-com:office:office#Keywords" & _
           Chr(34) & " is null"
      
      Set ml = ml.Items.Restrict(Filter)
      
      For i = ml.Count To 1 Step -1
      
            If TypeOf ml(i) Is MailItem Then
      
            End if
      
      Next
      

      【讨论】:

        【解决方案3】:

        效率可能没有明显的提高,但您可以应用第二个限制,而不是使用If TypeOf ml(i) Is MailItem Then 检查每个项目。

        Option Explicit
        
        Sub NullCategoryRestriction_MailItems()
        
            Dim oFolder As Folder
            Dim oItems As Items
            Dim ml As Items
            Dim i As Long
            
            Dim oFilter As String
            Dim oFilter2 As String
            
            Debug.Print
            'DASL Filter can test for null property.
            'This will return all items that have no category.
            ' https://docs.microsoft.com/en-us/office/vba/outlook/How-to/Search-and-Filter/filter-items-that-do-not-have-categories
            oFilter = "@SQL=" & Chr(34) & _
              "urn:schemas-microsoft-com:office:office#Keywords" & _
              Chr(34) & " is null"
            Debug.Print " " & oFilter
            
            Set oFolder = ActiveExplorer.CurrentFolder
            Set oItems = oFolder.Items.Restrict(oFilter)
            Debug.Print "   oItems.Count: " & oItems.Count
            
            'This will return mailitems
            ' https://docs.microsoft.com/en-us/office/vba/outlook/concepts/forms/item-types-and-message-classes
            oFilter2 = "[MessageClass] = 'IPM.Note'"
            Debug.Print " " & oFilter2
        
            Set ml = oItems.Restrict(oFilter2)
            Debug.Print "   ml.Count: " & ml.Count
            
            For i = ml.Count To 1 Step -1
                ' If TypeOf ml(i) Is mailItem Then
                    Debug.Print ml(i).MessageClass & ": " & ml(i).subject
                'End If
            Next
            
        End Sub
        

        不再需要 TypeOf 测试。

        【讨论】:

          猜你喜欢
          • 2018-01-09
          • 2020-11-12
          • 1970-01-01
          • 2017-06-22
          • 1970-01-01
          • 1970-01-01
          • 2022-01-15
          • 1970-01-01
          • 2021-03-17
          相关资源
          最近更新 更多