Error : Method 'Class' of Objject 'MailItem' Failed

  • Thread starter Thread starter Randy Lane
  • Start date Start date
R

Randy Lane

The following code worked flawlessly for ages on a Windows NT 4 Server
with Outlook 2000 accessing Exchange 5.5.
I recently installed Outlook 2002 on a Windows 2003 Server and copied
the macro. Now I get this wierd error all the time :

Method 'Class' of Objject 'MailItem' Failed

The error occurs on the last line of the code below. And idea what's
causing this?

Option Explicit
Private WithEvents oInboxItems As Items
Private Sub Application_Startup()
Dim oNS As NameSpace
Set oNS = Application.GetNamespace("MAPI")
Set oInboxItems = oNS.GetDefaultFolder(olFolderInbox).Items
Set oNS = Nothing
End Sub
Private Sub oInboxItems_ItemAdd(ByVal Item As Object)
Dim oApp As Application
Dim oNS As NameSpace
Dim oAttachments As Attachments
Dim oAttachedFile As Attachment
Dim oSNCFolder As MAPIFolder
Dim oSafeMail As Redemption.SafeMailItem
Dim oFilename As String
Dim oSaveAsName As String
Set oApp = Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oSNCFolder = oNS.Folders("SqlAdmin").Folders("State National
Autobill")
If Item.Class = olMail Then
 
Why are you instantiating an Application object? Just use the intrinsic
Application object provided in Outlook VBA. In a similar vein, declare your
NameSpace object at the module level so you only have to instantiate it once
or use Application.Session instead of getting a NameSpace object. I'd also
make sure that Outlook is referenced in the VBA project references and I'd
fully qualify any references such as Items using Outlook.Items.
 
Thanks Ken.
I changed/simplified teh code.
But I still get the same error.
I can drag a message to the Inbox and the code executes as expected,
but when a message comes into the Inbax from a Send/Receive execution,
the error occurs.
??

Here's teh code now:

Option Explicit
Private WithEvents oInboxItems As Items
Private Sub Application_Startup()
Set oInboxItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub oInboxItems_ItemAdd(ByVal Item As Object)
Dim oAttachments As Attachments
Dim oAttachedFile As Attachment
Dim oSNCFolder As MAPIFolder
Dim oSafeMail As Redemption.SafeMailItem
Dim oFilename As String
Dim oSaveAsName As String
Set oSNCFolder =
Application.GetNamespace("MAPI").Folders("SqlAdmin").Folders("State
National Autobill")
If Item.Class = olMail Then
 
Very strange. You're not running Outlook directly on an Exchange server box
are you?

If you set a breakpoint in your VBA code on the Item.Class line and test for
Item.MessageClass and TypeName(Item) what do you get? Can you retrieve
Item.EntryID and Item.Parent.StoreID as strings and then get the item as a
typed Mailitem using NameSpace.GetItemFromID(EntryID, StoreID)?
 
I am not running Outlook on the same box as Exchange.
But it is on the same box as an installation of MS Sql 2000.
Previously it all worked fine an a similar set up with Win NT 4, Sql
7, and OL 2000.
The new configuration is Win 2003, OL 2002, and Sql 2000.
I suspect the reason for the problem could be Sql 2000.
The code is in ThisOutlookSession.
It actually wokrs fine if I take the items in the Inbox, move them to
a temporary folder, then drag them back to the Inbox. It only fails
when the items arrive via a Send/Receive action.
I will try the breakpoint idea soon.
 
Back
Top