When is a MailItem not a MailItem? [closed]
Asked Answered
A

7

11

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.

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.

Annihilation answered 17/9, 2008 at 1:26 Comment(1)
H
13

This code showed me the different TypeNames that were in my Inbox:

Public Sub GetTypeNamesInbox()
Dim myOlItems As Outlook.Items
Set myOlItems = application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
Dim msg As Object

For Each msg In myOlItems
    Debug.Print TypeName(msg)
    'emails are typename MailItem
    'Meeting responses are typename MeetingItem
    'Delivery receipts are typename ReportItem
Next msg

End Sub

HTH

Honniball answered 29/12, 2011 at 19:3 Comment(0)
G
5

I use the following VBA code snippet in other Office Applications, where the Outlook Library is directly referenced.

' Outlook Variables

  Dim objOutlook As Outlook.Application: Set objOutlook = New Outlook.Application
  Dim objNameSpace As Outlook.NameSpace: Set objNameSpace = objOutlook.GetNamespace("MAPI")
  Dim objFolder As MAPIFolder: Set objFolder = objNameSpace.PickFolder()
  Dim objMailItem As Outlook.MailItem

  Dim iCounter As Integer:  iCounter = objFolder.Items.Count
  Dim i As Integer

  For i = iCounter To 1 Step -1
    If TypeOf objFolder.Items(i) Is MailItem Then
      Set objMailItem = objFolder.Items(i)
      With objMailItem

etc.

Generally answered 29/8, 2012 at 18:10 Comment(3)
I like the TypeOf better than hard-coding "MailItem" as a string.Dyaus
Good answer but in my case I had to check also the MessageClass to exclude the case of IPM.Outlook.Recall that has basically no properties defined except the SubjectSkylar
Based on ms doc, for Email messages you should verify the condition If TypeOf objFolder.Items(i) Is MailItem And objFolder.Items(i).MessageClass = "IPM.Note" ThenSkylar
S
3

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.

Here is the code stripped of the functionality, but giving you an idea of how it looks:

   Public WithEvents myOlItems As Outlook.Items

   Public Sub Application_Startup()
       ' Reference the items in the Inbox. Because myOlItems is declared
       ' "WithEvents" the ItemAdd event will fire below.
       ' Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
       Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
   End Sub

   Private Sub myOlItems_ItemAdd(ByVal Item As Object)
       On Error Resume Next
       If TypeName(Item) = "MailItem" Then
           MyMessageHandler Item
       End If
   End Sub

   Public Sub MyMessageHandler(ByRef Item As MailItem)
       Dim strSender As String
       Dim strSubject As String

       If TypeName(Item) <> "MailItem" Then
           Exit Sub
       End If

       strSender = LCase(Item.SenderEmailAddress)
       strSubject = Item.Subject

       rem do stuff
       rem do stuff
       rem do stuff
   End Sub

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?

Another one I get is where an email with an empty subject comes along. The line

strSubject = Item.Subject

gives me an error. I know Item.Subject should be blank, but why is that an error?

Thanks.

Skyjack answered 17/9, 2008 at 1:39 Comment(2)
TypeName interrogates a type for a human-readable version. Since there can be two different types with the same name, relying on TypeName to do type checking will result in false-positives/negatives. Try "TypeOf Item is MailItem" insteadMunroe
If TypeName(Item) = "MailItem", then call a procedure ... which not only accepts only MailItems, but again checks if the item is a MailItem?Tremayne
D
2

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.)

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.

I'll see if I can produce a specific example if it will help.

Derrick answered 29/9, 2008 at 20:53 Comment(0)
T
1

There are many types of items that can be seen in the default Inbox.

In the called procedure, assign the incoming item to an Object type variable. Then use TypeOf or TypeName to determine if it is a MailItem. Only then should your code perform actions that apply to emails.

i.e.

Dim obj As Object

If TypeName(obj) = "MailItem" Then
  ' your code for mail items here
End If
Tremayne answered 21/6, 2012 at 19:52 Comment(0)
S
1
Dim objInboxFolder As MAPIFolder
Dim oItem As MailItem
Set objInboxFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

For Each Item In objInboxFolder.Items
    If TypeName(Item) = "MailItem" Then
    Set oItem = Item

next
Symons answered 24/7, 2012 at 15:36 Comment(1)
Please add an explanation to your code.Porta
R
0

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.

On Error Resume Next
If object.subject = Null 'produces an error when subject is null, otherwise allows a good read
  strSubject = ""   'sets the subject grab string to a null or empty string as a string
Else
 strSubject = object.subject 'Sets the subject grab string to the subject of the message\item
End If
Rebato answered 27/4, 2013 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.