<p>I have written a message handler function in Outlook's Visual Basic (we're using Outlook 2003 and Exchange Server) to help me sort out incoming email. </p>

<p>It is working for me, except sometimes the rule fails and Outlook deactivates it. </p>

<p>Then I turn the rule back on and manually run it on my Inbox to catch up. The rule spontaneously fails and deactivates several times a day. </p>

<p>I would love to fix this once and for all.</p>

[{'answer_id': 79000, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>have written a message handler function in Outlook\'s Visual Basic (we\'re using Outlook 2003 and Exchange Server) to help me sort out incoming email. It is working for me, except sometimes the rule fails and Outlook deactivates it. Then I turn the rule back on and manually run it on my Inbox to catch up. The rule spontaneously fails and deactivates several times a day. I would love to fix this once and for all.</p>\n\n<p>Here is the code stripped of the functionality, but giving you an idea of how it looks:</p>\n\n<pre><code>   Public WithEvents myOlItems As Outlook.Items\n\n   Public Sub Application_Startup()\n       \' Reference the items in the Inbox. Because myOlItems is declared\n       \' "WithEvents" the ItemAdd event will fire below.\n       \' Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items\n       Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items\n   End Sub\n\n   Private Sub myOlItems_ItemAdd(ByVal Item As Object)\n       On Error Resume Next\n       If TypeName(Item) = "MailItem" Then\n           MyMessageHandler Item\n       End If\n   End Sub\n\n   Public Sub MyMessageHandler(ByRef Item As MailItem)\n       Dim strSender As String\n       Dim strSubject As String\n\n       If TypeName(Item) &lt;&gt; "MailItem" Then\n           Exit Sub\n       End If\n\n       strSender = LCase(Item.SenderEmailAddress)\n       strSubject = Item.Subject\n\n       rem do stuff\n       rem do stuff\n       rem do stuff\n   End Sub\n</code></pre>\n\n<p>One error I get is "Type Mismatch" calling MyMessageHandler where VB complains that Item is not a MailItem. Okay, but TypeName(Item) returns "MailItem", so how come Item is not a MailItem?</p>\n\n<p>Another one I get is where an email with an empty subject comes along. The line</p>\n\n<pre><code>strSubject = Item.Subject\n</code></pre>\n\n<p>gives me an error. I know Item.Subject should be blank, but why is that an error?</p>\n\n<p>Thanks.</p>\n'}, {'answer_id': 150607, 'author': 'Dave DuPlantis', 'author_id': 8174, 'author_profile': 'https://Stackoverflow.com/users/8174', 'pm_score': 2, 'selected': False, 'text': "<p>My memory is somewhat cloudy on this, but I believe that a MailItem is not a MailItem when it is something like a read receipt. (Unfortunately, the VBA code that demonstrated this was written at another job and isn't around now.)</p>\n\n<p>I also had code written to process incoming messages, probably for the same reason you did (too many rules for Exchange, or rules too complex for the Rules Wizard), and seem to recall running into the same problem you have, that some items seemed to be from a different type even though I was catching them with something like what you wrote.</p>\n\n<p>I'll see if I can produce a specific example if it will help.</p>\n"}, {'answer_id': 8672202, 'author': 'Killian Tyler', 'author_id': 1121762, 'author_profile': 'https://Stackoverflow.com/users/1121762', 'pm_score': 4, 'selected': False, 'text': '<p>This code showed me the different TypeNames that were in my Inbox:</p>\n\n<pre><code>Public Sub GetTypeNamesInbox()\nDim myOlItems As Outlook.Items\nSet myOlItems = application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items\nDim msg As Object\n\nFor Each msg In myOlItems\n    Debug.Print TypeName(msg)\n    \'emails are typename MailItem\n    \'Meeting responses are typename MeetingItem\n    \'Delivery receipts are typename ReportItem\nNext msg\n\nEnd Sub\n</code></pre>\n\n<p>HTH</p>\n'}, {'answer_id': 11145745, 'author': 'JimmyPena', 'author_id': 190829, 'author_profile': 'https://Stackoverflow.com/users/190829', 'pm_score': 1, 'selected': False, 'text': '<p>There are many types of items that can be seen in the default Inbox.</p>\n\n<p>In the called procedure, assign the incoming item to an <code>Object</code> type variable. Then use <code>TypeOf</code> or <code>TypeName</code> to determine if it is a <code>MailItem</code>. Only then should your code perform actions that apply to emails.</p>\n\n<p>i.e.</p>\n\n<pre><code>Dim obj As Object\n\nIf TypeName(obj) = "MailItem" Then\n  \' your code for mail items here\nEnd If\n</code></pre>\n'}, {'answer_id': 11634371, 'author': 'Radek', 'author_id': 1549220, 'author_profile': 'https://Stackoverflow.com/users/1549220', 'pm_score': 1, 'selected': False, 'text': '<pre><code>Dim objInboxFolder As MAPIFolder\nDim oItem As MailItem\nSet objInboxFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)\n\nFor Each Item In objInboxFolder.Items\n    If TypeName(Item) = "MailItem" Then\n    Set oItem = Item\n\nnext\n</code></pre>\n'}, {'answer_id': 12183994, 'author': 'Bruce E. Leandro', 'author_id': 1634032, 'author_profile': 'https://Stackoverflow.com/users/1634032', 'pm_score': 2, 'selected': False, 'text': '<p>I use the following VBA code snippet in other Office Applications, where the Outlook Library is directly referenced.</p>\n\n<pre><code>\' Outlook Variables\n\n  Dim objOutlook As Outlook.Application: Set objOutlook = New Outlook.Application\n  Dim objNameSpace As Outlook.NameSpace: Set objNameSpace = objOutlook.GetNamespace("MAPI")\n  Dim objFolder As MAPIFolder: Set objFolder = objNameSpace.PickFolder()\n  Dim objMailItem As Outlook.MailItem\n\n  Dim iCounter As Integer:  iCounter = objFolder.Items.Count\n  Dim i As Integer\n\n  For i = iCounter To 1 Step -1\n    If TypeOf objFolder.Items(i) Is MailItem Then\n      Set objMailItem = objFolder.Items(i)\n      With objMailItem\n</code></pre>\n\n<p>etc.</p>\n'}, {'answer_id': 16250499, 'author': 'htd', 'author_id': 2234464, 'author_profile': 'https://Stackoverflow.com/users/2234464', 'pm_score': 0, 'selected': False, 'text': '<p>why not use a simple error handler for the code?  Seriously.  You could write an error for each read of a property or object that seems to fail.  Then have it Resume no matter what.  No need for complex error handling.  Think of a test that shows an empty subject.  Since you don\'t know what value it will return, if any, and it seems to error on an empty or blank subject, you need to picture it as a simple test with a possible error. Run the test as an if statement (one in which you will get an error anyway), and have the program resume on error.<br/></p>\n\n<pre><code>On Error Resume Next\nIf object.subject = Null \'produces an error when subject is null, otherwise allows a good read\n  strSubject = ""   \'sets the subject grab string to a null or empty string as a string\nElse\n strSubject = object.subject \'Sets the subject grab string to the subject of the message\\item\nEnd If\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78924', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']