问题
I'm trying to get a count of how many emails are in my Outlook folders. The problem is that it is counting the "Flagged" items and I need the code to skip any item that is "Flagged".
I've tried using the "olNoFlag" property on line 18 of the below code but it won't work. Can anyone help me with this? I'm so close!
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:
Work with Items.Restrict Method (Outlook) to exclude flag Items by Filtering Items Using a String Comparison
Filter = "@SQL=" & " Not " & _
"http://schemas.microsoft.com/mapi/proptag/0x10900003" & _
"" & "=1"
Code Example
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 Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.
The method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found. _Filtering Items Using a String Comparison that DASL filters support includes equivalence, prefix, phrase, and substring matching. Note that when you filter on the Subject property, prefixes such as "RE: " and "FW: " are ignored.
来源:https://stackoverflow.com/questions/43641700/how-do-you-specify-a-flagged-item-in-outlook-with-excel-vba