0

I have VBA code in Outlook 2016 Pro Plus that has run for years.

All of a sudden, almost every time I do a Send & Receive I am getting an error on Set myEmail =.

Option Explicit
Option Base 1
Option Compare Text

Sub MyRules()

Set myNameSpace = GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

myEmails = myInbox.Items.Count

For myCount = myEmails To 1 Step -1
    Set myEmail = myInbox.Items(myCount) '**<<< Error occurs on this line <<<**
    Select Case myEmail.SenderEmailAddress
        Case Is = "aaa@gmail.com", "bbb@btinternet.com", "ccc@gmail.com"
            mySender = "Cxxxxx Dxxxxxx"
        Case Else
            mySender = myEmail.SenderEmailAddress
    End Select
    Select Case mySender
        Case Is = ""
            If myEmail.Subject = "" Then
                myEmail.UnRead = False
                myEmail.Delete
                GoTo Next_EMail
            End If

The error

This should be all of the relevant DIM statements.

Public myNameSpace As NameSpace
Public myInbox As MAPIFolder
Public myDest As MAPIFolder
Public myEmail As MailItem
Public myEmails As Integer
Public myCount As Integer
Public myAtmt As Attachment
Public myAtmtNo As Integer
Public mySender As String
Public myRecip As Outlook.Recipient
Public mySubFolders As Outlook.Folders
Public mySubFolder As MAPIFolder
Public mySubFolder1 As MAPIFolder
Public mySubFolder2 As MAPIFolder
Public mySubFolder3 As MAPIFolder
Public myNS As NameSpace
Public myTestFolder As Outlook.Folder
Public myFoldersArray As Variant
Public myLoopIndex As Integer
Public myOlApp As Outlook.Application
Public myOlTsk As TaskItem
Public myNoOfEMails As Long
Community
  • 1
  • 1
Gary Heath
  • 313
  • 1
  • 3
  • 15
  • 1
    What did you declare `myEmail` as? Most likely you're dealing with an item that is not a `MailItem`... not all items are emails, i.e. see [this](https://stackoverflow.com/questions/78924/when-is-a-mailitem-not-a-mailitem). – BigBen Jul 07 '20 at 13:57
  • Where are the `Dim` statements? You have `Option Explicit`. Please add these statements to the code in your question – Super Symmetry Jul 07 '20 at 15:12
  • Thanks BigBen, it's set "Public myEmail As MailItem", and so far I've noticed that this is happening on things like NoReply@Tesco.com, hello@news.monese.com & reply@e.seetickets.com, but all of these have been in my Inbox for ages and must have gone through this code thousands of times !!! It always used to crash if I received a Read Notification, but only those, nothing else ... – Gary Heath Jul 07 '20 at 15:15

2 Answers2

1

Try to test the type with If TypeName(myInbox.Items(myCount)) = "MailItem" Then and see if it works:

Option Explicit
Option Base 1
Option Compare Text

Sub MyRules()

Set myNameSpace = GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)

myEmails = myInbox.Items.Count

For myCount = myEmails To 1 Step -1
    If TypeName(myInbox.Items(myCount)) = "MailItem" Then
        Set myEmail = myInbox.Items(myCount) '**<<< Error occurs on this line <<<**
        Select Case myEmail.SenderEmailAddress
            Case Is = "aaa@gmail.com", "bbb@btinternet.com", "ccc@gmail.com"
                mySender = "Cxxxxx Dxxxxxx"
            Case Else
                mySender = myEmail.SenderEmailAddress
        End Select
        Select Case mySender
            Case Is = ""
                If myEmail.Subject = "" Then
                    myEmail.UnRead = False
                    myEmail.Delete
                    GoTo Next_EMail
                End If

Edit: follow-up to comments

Add an Else clause such as:

    If TypeName(myInbox.Items(myCount)) = "MailItem" Then
        Set myEmail = myInbox.Items(myCount) '**<<< Error occurs on this line <<<**
        '... the rest of the code
    
    Else
        Debug.Print TypeName(myInbox.Items(myCount))
        '... Some other prints here
    End if
Super Symmetry
  • 2,837
  • 1
  • 6
  • 17
  • This seems to work, thank you, but why has this suddenly started happening, the code has been happily running without that line or this problem for years ?!?!? – Gary Heath Jul 07 '20 at 16:19
  • I can only speculate, but it is possible that some other code saves other types of `Item`s for archiving purposes and so hapened to save a non-`MailItem` in the inbox folder. Try looping through your Inbox until you find a non-`MailItem` and display some of its properties in a msgbox. That might lead you to the root of the problem. – Super Symmetry Jul 07 '20 at 16:25
1

An Outlook folder may contain different types of items. So, you need to check out the item type first (or message class). For example:

Dim obj As Object

If TypeName(obj) = "MailItem" Then
  ' your code for mail items here
End If

Also, I have noticed the following loop in the code:

For myCount = myEmails To 1 Step -1

It is not really ya good ides to iterate over all items in the Inbox folder. Instead, I'd recommend using the Find/FindNext or Restrict methods of the Items class to get a collection of items that correspond to your conditions only. Read more about these methods in the following articles:

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you, I've just accepted Super Symmetry's similar answer but yours gives the same basic reason ... however, as I've asked him, why would this problem suddenly spring up when this code has been working OK for years ?!? I'll also look into the loop, as my Inbox can get very large, thanks :-) – Gary Heath Jul 07 '20 at 16:22