简体   繁体   中英

Count Followup Emails using Excel VBA

I am using Office 2013 and I am trying to get a count of the followup items in one of my email folders and this value will be written into a cell.

So I am using the below code after adding the Outlook Object Library reference:

Dim Folder As Outlook.MAPIFolder

Dim objOL As Outlook.Application
Set objOL = New Outlook.Application

MailboxName = "mymailboxhere"
Main_Folder_Name = "Inbox"
Sub_Folder_Name = "Test"

Set Folder = Outlook.Session.Folders(MailBoxName).Folders(Main_Folder_Name).Folders(Sub_Folder_Name)

Dim itms As Outlook.Items
Set itms = Folder.Items

Dim FollowupItms As Outlook.Items
Set FollowupItms = itms.Restrict("[FlagStatus] = 2")

Followup = FollowupItms.Count
Worksheets("Sheet1").Range("A1").Value = Followup

For some reason I keep getting the Followup count as 0 even though there is at least an email flagged as followup.

To test I have tried the below:

For Each Msg In itms
MsgBox Msg.FlagStatus
Next

And the Flagstatus of one of the emails is 2 and the same shows in the Msgbox during the test.

The code works fine when counting emails that are UnFlagged or emails that are marked as Completed.

This makes absolutely no sense to me. Any thoughts?

MSDN says the OlFlagStatus enumeration:

... is deprecated and is not intended to be used in your code.

(See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.olflagstatus(v=office.14).aspx for details)

Instead, try the MailItem.FlagRequest property. Eg

Set FollowupItms = itms.Restrict("[FlagRequest] = 'Follow up'")

You can find info on the MailItem.FlagRequest property on MSDN at http://msdn.microsoft.com/en-us/library/office/ff861323(v=office.14).aspx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM