Outlook Count Emails mark as Important

こ雲淡風輕ζ 提交于 2019-12-13 03:52:03

问题


Can someone point out what I am missing here. Every time I run this it says that an object is required.

I apologize I feel like this is a very easy fix but I have been wrecking my brain for a while.

Basically what I am trying to accomplish is count how many emails are mark as high importance.

Again I feel like this is such a simple error but I am still learning this.

Sub CheckForImportance()

Dim myNs As Outlook.NameSpace
Dim infldr  As Outlook.Folder
Dim impMail As Outlook.MailItem
Dim ttlcount As Integer

Set myNs = Application.GetNamespace("MAPI")
Set infldr = myNs.GetDefaultFolder(olFolderInbox)
Set impMail = infldr.Items
Set ttlcount = 0

If impMail.Importance = olImportanceHigh Then ttlImp = ttlImp + 1

MsgBox ("count:" & ttlImp)

End Sub

回答1:


Outlook stores mail items, calendar items, tasks and so on in files it calls Stores. Sometimes people say mail items and so on are stored in PST files which is usually true. However, all PST files are stores but not all stores are PST files.

I remember when the default was for messages sent to any of your email addresses to be loaded to the same store. In that situation, Set infldr = myNs.GetDefaultFolder(olFolderInbox)was useful since the default Inbox was in that one store.

With Outlook 2016, and perhaps some other recent versions, the default is to have a separate store for each email address. Each of these stores is named for the email address, for example: “JohnDoe@hotmail.com” or “DoeJ@gmail.com”.

Copy this macro to an Outlook module and run it:

Sub DsplUsernameOfDefaultStore()

  Dim NS As Outlook.NameSpace
  Dim DefaultInboxFldr As MAPIFolder

  Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
  Set DefaultInboxFldr = NS.GetDefaultFolder(olFolderInbox)

  Debug.Print DefaultInboxFldr.Parent.Name

End Sub

On my system, this macro outputs “Outlook Data File”. This was the default store that came with Outlook but none of my emails are loaded to it.

You will need something like:

Set infldr = Session.Folders("Xxxx").Folders("Inbox")

where Xxxx is the name of the store containing the Inbox you wish to interrogate.

Below I have three alternative macros that count the number of high importance emails in an Inbox. Points of particular note:

  • Version 1 uses a For Each loop as I suggested in my comment. Version 2 uses a For IndexVariable loop. To my knowledge, neither type of For has an advantage over the other. I use whichever seems more convenient for the task at hand. Version 3 uses a filter. I have not found a use for the Outlook filter often enough to have become expert in its use so I normally use a For loop. olImportanceHigh is a constant with a value of 2. It appears you cannot use a constant within a Restrict string which is why it says [Importance] = 2.
  • I find Debug.Print much more convenient than MsgBox during development.

Come back with questions about my code as necessary.

Option Explicit
Sub CountHighImportanceEmails1()

  Dim FldrInbox As Folder
  Dim MailItemCrnt As MailItem
  Dim NumEmailsHighImport As Long

  Set FldrInbox = Session.Folders("Xxxx").Folders("Inbox")

  NumEmailsHighImport = 0
  For Each MailItemCrnt In FldrInbox.Items
    If MailItemCrnt.Importance = olImportanceHigh Then
      NumEmailsHighImport = NumEmailsHighImport + 1
    End If
  Next

  Debug.Print "Number of high importance emails=" & NumEmailsHighImport

End Sub
Sub CountHighImportanceEmails2()

  Dim FldrInbox As Folder
  Dim InxMi As Long
  Dim NumEmailsHighImport As Long

  Set FldrInbox = Session.Folders("Xxxx").Folders("Inbox")

  NumEmailsHighImport = 0
  With FldrInbox
    For InxMi = 1 To .Items.Count
      If .Items(InxMi).Importance = olImportanceHigh Then
        NumEmailsHighImport = NumEmailsHighImport + 1
      End If
    Next
  End With

  Debug.Print "Number of high importance emails=" & NumEmailsHighImport

End Sub
Sub CountHighImportanceEmails3()

  Dim FldrInbox As Folder
  Dim MailItemsHighImport As Items

  Set FldrInbox = Session.Folders("Xxxx").Folders("Inbox")

  Set MailItemsHighImport = FldrInbox.Items.Restrict("[Importance] = 2")

  Debug.Print "Number of high importance emails=" & MailItemsHighImport.Count

End Sub



回答2:


Example would be

Option Explicit
Public Sub Example()
    Dim Inbox As Outlook.folder
    Set Inbox = Application.Session.GetDefaultFolder( _
                                    olFolderInbox)

    Dim Filter As String
        Filter = "[Importance] = 2"

    Dim Items As Outlook.Items
    Set Items = Inbox.Items.Restrict(Filter)

    Debug.Print Items.Count
    MsgBox Items.Count & " High importance Items are in " & Inbox.Name

End Sub


来源:https://stackoverflow.com/questions/44665683/outlook-count-emails-mark-as-important

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!