Consulting

Results 1 to 3 of 3

Thread: Trying to get macro to call on unread emails only

  1. #1
    VBAX Newbie
    Joined
    Aug 2023
    Posts
    2
    Location

    Trying to get macro to call on unread emails only

    Hopefully an easy one.

    I'm trying to get a macro to run against unread emails only. I have the below so far but it only runs on the selected email, not all unread emails:

    Public Sub Unread_Emails() 
    Dim myNameSpace As Outlook.NameSpace
    Dim myInbox As Outlook.Folder
    Dim myItem As MailItem
    
    
    Dim lngCount As Long
        Set myNameSpace = Application.GetNamespace("MAPI")
        Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    
    
        MsgBox myInbox.UnReadItemCount
        If myInbox.UnReadItemCount > 0 Then
            For lngCount = myInbox.items.Count To 1 Step -1
                If TypeName(myInbox.items(lngCount)) = "MailItem" Then
                    If myInbox.items(lngCount).UnRead = True Then
                        Set myItem = myInbox.items(lngCount)
                        Call my_Sub
    
    
    
    
                        End If
                    End If
                End If
                DoEvents
            Next lngCount
        End If
        Set myNameSpace = Nothing
        Set myInbox = Nothing
        Set myItem = Nothing
        Set myDestFolder = Nothing
    End Sub

    What do I need to change to get this to only operate on unread emails?

  2. #2
    The code you posted has an extra End If line, but that aside it does act on unread items in the Inbox. The burning question relates to my_Sub, which you haven't shared. It seems likely that any issues relating to the processing should be found there.
    Option Explicit
    
    Public Sub Unread_Emails()
    Dim myNameSpace As Outlook.NameSpace
    Dim myInbox As Outlook.Folder
    Dim myItem As MailItem
    Dim lngCount As Long
        Set myNameSpace = Application.GetNamespace("MAPI")
        Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    
    
        MsgBox myInbox.UnReadItemCount
        If myInbox.UnReadItemCount > 0 Then
            For lngCount = myInbox.Items.Count To 1 Step -1
                If TypeName(myInbox.Items(lngCount)) = "MailItem" Then
                    If myInbox.Items(lngCount).UnRead = True Then
                        Set myItem = myInbox.Items(lngCount)
                        Debug.Print myItem.Subject
                        'Call my_Sub
                    End If
                End If
                DoEvents
            Next lngCount
        End If
        Set myNameSpace = Nothing
        Set myInbox = Nothing
        Set myItem = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Aug 2023
    Posts
    2
    Location
    Thanks Graham - I think you have answered my question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •