VBA Code being completely ignored by Outlook

Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#1
I have entered some code lifted from the Slipstick site and pasted it into the "ThisOutlookSession" area. Basically, all it is supposed to do is just popup a msgbox when an email arrives.

I have relaunched Outlook to pick up the new code, but nothing happens when an email arrives. I simply cannot believe that it is this hard to just display a message! I have the feeling that Outlook is ignoring the code altogether - we are using the cloud (Office 365) version. Is this why.

Getting so frustrated with this!

Can anyone give me a steer with this

thanks!
 
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#3
Are macros enabled? File, Options, Trust Center, Macros. Set it to enable all to test.
Hi Diane,

Thanks for your response.

I have now set the Trust Centre on Macros to "Enable all macros (not recommended.....blah)

I have restarted Outlook twice now, but nothing happens when I receive an email.

The VBA code is lifted from your website:

Option Explicit

Private objNS As Outlook.NameSpace

Private WithEvents objNewMailItems As Outlook.Items

Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder

Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objNewMailItems = objMyInbox.Items
Set objMyInbox = Nothing

End Sub

Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
Dim objEmail As Outlook.MailItem
'Ensure we are only working with e-mail items
If Item.Class <> OlItemType.olMailItem Then Exit Sub

MsgBox "Message subject: " & objEmail.Subject & " inro ASK Finance mailbox", vbCritical

'Debug.Print "Message sender: " & objEmail.SenderName & " (" & objEmail.SenderEmailAddress & ")"
Set objEmail = Nothing

End Sub

And is pasted into ThisOutlookSession folder.I'm expecting to get the normal VB msgbox popup in the middle of the screen.

I don't really even know how I can test the VBA ie. Make it do something to definitely confirm is awake and talking.

Any ideas?

many thanks
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#4
This line is not working -

If Item.Class <> OlItemType.olMailItem Then Exit Sub

The last sub that does the work is
Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
MsgBox "Message subject: " & Item.Subject & " inro ASK Finance mailbox", vbCritical
End Sub

are you trying to watch a secondary mailbox? You'll need to tell it to watch that folder - Working with VBA and non-default Outlook Folders - Slipstick Systems - but I'm not 100% sure it will work.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#5
This works with secondary accounts when the mailbox is open in the profile - the mailbox name as it appears in the folder list goes into the line marked bold (right click on the inbox in the folder, choose properties and copy the mailbox name ). And you need the GetFolderPath function from the link I posted earlier.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items
Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder
Set objNewMailItems = GetFolderPath("Forum Replies\Inbox").Items

Set objMyInbox = Nothing
End Sub
Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
'If Item.Class <> OlItemType.olMailItem Then Exit Sub
MsgBox "Message subject: " & Item.Subject & " into ASK Finance mailbox", vbCritical
End Sub
 
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#6
This works with secondary accounts when the mailbox is open in the profile - the mailbox name as it appears in the folder list goes into the line marked bold (right click on the inbox in the folder, choose properties and copy the mailbox name ). And you need the GetFolderPath function from the link I posted earlier.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objNewMailItems As Outlook.Items
Private Sub Application_Startup()
Dim objMyInbox As Outlook.MAPIFolder
Set objNewMailItems = GetFolderPath("Forum Replies\Inbox").Items

Set objMyInbox = Nothing
End Sub
Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
'If Item.Class <> OlItemType.olMailItem Then Exit Sub
MsgBox "Message subject: " & Item.Subject & " into ASK Finance mailbox", vbCritical
End Sub
Thanks once again Diane.

However, setting up code in the VBA window is not much good if Office 365 is suppressing execution.

How can I test this?

Can i for instance pop-up a msgbox when I launch Outlook? That would certainly

indicate whether or not VBA is being processed properly. t6rouble is, i do not know the event

or what things I have to set up to acheive this.

Thanks
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#7
Office365 hosted Exchange? That is what I tested it with - my mailbox and a managed mailbox in my profile (it's added automatically when I create the profile - an automated version of Account Settings, double click on my account, More Settings then add a secondary mailbox).

If you are opening it using Open other users folder, I'm not sure a macro will work on it.

Application_startup is the one that runs on start up.

Code:
Private Sub Application_Startup() 
 
msgbox "Welcome" 
 
Dim objMyInbox As Outlook.MAPIFolder 
 
Set objNewMailItems = GetFolderPath("Forum Replies\Inbox").Items 
 
Set objMyInbox = Nothing 
 
End Sub
 
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
#8
Office365 hosted Exchange? That is what I tested it with - my mailbox and a managed mailbox in my profile (it's added automatically when I create the profile - an automated version of Account Settings, double click on my account, More Settings then add a secondary mailbox).

If you are opening it using Open other users folder, I'm not sure a macro will work on it.

Application_startup is the one that runs on start up.

Code:
Private Sub Application_Startup() 
 
msgbox "Welcome" 
 
Dim objMyInbox As Outlook.MAPIFolder 
 
Set objNewMailItems = GetFolderPath("Forum Replies\Inbox").Items 
 
Set objMyInbox = Nothing 
 
End Sub
Many thanks indeed for all your time and effort - I've now managed to get this working.
 
Top