如何使用Excel VBA在Outlook中指定标记项?

时间:2017-04-26 18:17:34

标签: vba excel-vba excel-2010 outlook-vba outlook-2010

我正在尝试计算Outlook文件夹中有多少封电子邮件。问题是它正在计算“已标记”项目,我需要代码跳过任何“已标记”的项目。

我尝试在下面代码的第18行使用“olNoFlag”属性,但它不起作用。谁能帮我这个?我太近了!

Sub LoopFoldersInNoctalkSW()

Dim ns As Object
Dim objFolder As Object
Dim objSubfolder As Object
Dim lngCounter As Long
Dim olNoFlag As Object

Set ns = GetObject("", "Outlook.Application").GetNamespace("MAPI")
Set objFolder = ns.Folders("NoctalkSW")

For Each objSubfolder In objFolder.Folders
On Error Resume Next
With Worksheets("Folder Names 2")
    lngCounter = lngCounter + 1
    .Cells(lngCounter, 1) = objSubfolder.Name
    .Cells(lngCounter, 2) = objSubfolder.Items.Count
    .Cells(lngCounter, 3) = objSubfolder.Items.GetLast.ReceivedTime
End With

Debug.Print objSubfolder.Name
Debug.Print objSubfolder.Items.Count
Debug.Print objSubfolder.Items.GetLast.ReceivedTime

Next objSubfolder

End Sub

1 个答案:

答案 0 :(得分:0)

使用 Items.Restrict Method (Outlook) Filtering Items Using a String Comparison

排除标记项目
Filter = "@SQL=" & " Not " & _
                   "http://schemas.microsoft.com/mapi/proptag/0x10900003" & _
                   "" & "=1"

代码示例

Option Explicit
Const olFolderInbox = 6
Sub HowManyEmails()
    Dim olApp As Object
    Dim olNs As Object
    Dim Inbox As Object
    Dim SubFolder As Object
    Dim Recip As Object
    Dim Items As Object
    Dim Filter As String

    Set olApp = CreateObject("Outlook.Application")
    Set olNs = olApp.GetNamespace("MAPI")
    Set Recip = olNs.CreateRecipient("0m3r@email.com") ' Share address
        Recip.Resolve
    Set Inbox = olNs.GetSharedDefaultFolder(Recip, olFolderInbox) ' Inbox

    Filter = "@SQL=" & " Not " & _
                       "http://schemas.microsoft.com/mapi/proptag/0x10900003" & _
                       "" & "=1"

    Set Items = Inbox.Items.Restrict(Filter) ' filter inbox items

    '// Print on Immediate Window
    Debug.Print Inbox.Name & " Has " & Items.Count & " Items "

    For Each SubFolder In Inbox.Folders
        Set Items = SubFolder.Items.Restrict(Filter) ' filter sub items

        '// Print on Immediate Window
        Debug.Print SubFolder.Name & " Has " & Items.Count & " Items "
    Next

    Set olApp = Nothing
    Set olNs = Nothing
    Set Inbox = Nothing
    Set SubFolder = Nothing
    Set Recip = Nothing

End Sub
  

Items.Restrict Method 对Items集合应用过滤器,返回一个新集合,其中包含原始中与过滤器匹配的所有项目。
  该方法是使用 Find method FindNext method 来迭代集合中特定项目的替代方法。如果项目数量较少, Find or FindNext methods 比过滤更快。如果集合中有大量项目,则Restrict方法会明显加快,特别是如果预计只能找到大集合中的少数项目。
  _

           

Filtering Items Using a String Comparison DASL 过滤器支持包括等效,前缀,短语和子字符串匹配。请注意,当您对Subject属性进行过滤时,将忽略“RE:”“FW:”等前缀。