VBA Macro - Run-time error '424': object required - Help Please

Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
#1
Hi,

I'm afraid I'm no developer, but I'm trying to cobble together a small automation solution in Outlook for myself, using some example code I found on the web (which originated from Sue Mosher), but I'm afraid I've stumbled at the very first hurdle.

I want to get Outlook to 'do something' within the current, open email form, each time I create a new email, or perform Reply, or Reply All, or Forward on an existing email. At this point, I don't think the 'something' is important, I'm hitting my problem before even getting that far! Here is my code, as it stands:

Dim WithEvents colInsp As Outlook.Inspectors

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objMail As Outlook.MailItem
If Inspector.CurrentItem.Class = olMail Then
Set objMail = Inspector.CurrentItem.Class
If objMail.Sent = False Then
MsgBox "Hello World", vbOKOnly, "Debug Message"
End If
End If
Set objMail = Nothing
End Sub

I replaced an original line from the example code with the line:
MsgBox "Hello World", vbOKOnly, "Debug Message"

I did this on the expectation that when I clicked New Email, or Reply etc., the email form would open and I'd then see the message box, just to prove to myself that the macro would execute as I was hoping/expected.

The macro certainly seems to begin to execute when I create a new email or begin a reply, but it fails at the line:
Set objMail = Inspector.CurrentItem.Class

The error message is:
Run-time error '424': object required

It would seem that the eror is occurring before the line I replaced, so I'm thinking that my line is not the problem here. I'm using Outlook 2016, so I wonder if there might be a compatibility issue with the code, which probably predates this version? If that's not it, then I'm wondering if maybe I've simply not created the macro within Outlook in the correct way, or in the correct location, or with the correct properties, or something like that?

If anyone could provide me with some assistance here, I'd be very grateful. I think that once I have the basic functionality working, I'm hoping I will be able to create the rest of the macro (which will do the thing I want it to do) without needing any further assistance. Many thanks, in advance.

Kind regards,

Steve
 
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
#3
Hi Diane,

Brilliant, it works! Makes me wonder why that '.Class' was in the original example, but I guess it could just have been a typo.

Anyway, many thanks indeed for your help, I can now get on with trying to acheive the primary objective!

Kind regards,

Steve
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#4
Year, most likely a typo or they copied it from the line above and forgot to remove it - i've done that before. :(
 
Top