VBA Code being completely ignored by Outlook

Status
Not open for further replies.

paulricot

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
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!
 
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
 
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.
 
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
 
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
 
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
 
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.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
D VBA code to select a signature from the signatures list Outlook VBA and Custom Forms 3
S Excel vba code to manage outlook web app Using Outlook 10
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
S Add VBA save code Using Outlook 0
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
S Skype for business meeting vba code Outlook VBA and Custom Forms 1
R Expand VBA Permanent Delete Code Outlook VBA and Custom Forms 6
B Outlook Business Contact Manager with SQL to Excel, User Defined Fields in BCM don't sync in SQL. Can I use VBA code to copy 1 field to another? BCM (Business Contact Manager) 0
A VBA Code in Outlook disappears after first use Outlook VBA and Custom Forms 1
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
R VBA Code to permanently delete selected email Outlook VBA and Custom Forms 10
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
S VBA Code to move mail items from search folder to inbox subfolder Outlook VBA and Custom Forms 4
B VBA Code to create appointment from email Outlook VBA and Custom Forms 1
D VBA Code to strip Subject Line when replying or forwarding Using Outlook 3
M VBA Code to Restart Outlook on error Outlook VBA and Custom Forms 3
Z Default VBA code for extracting data from email (Outlook) to Excel? Outlook VBA and Custom Forms 1
T VBA to Sort Rules [A-Z] - code provided Outlook VBA and Custom Forms 9
M VBA Auto-Reply code for Two Mailboxes on one Outlook Session. Outlook VBA and Custom Forms 4
Z Protecting VBA code - anything new? Outlook VBA and Custom Forms 2
G VBA code to enable a rule based on time of day for a IMAP mail account Outlook VBA and Custom Forms 14
S VBA code to rename a task (flagged message) Outlook VBA and Custom Forms 1
M VBA code to save email attachments (PDF) as email subject line Outlook VBA and Custom Forms 1
F VBA Code to change subject Like Outlook VBA and Custom Forms 3
Diane Poremsky Pasting VBA code (and other editing) Outlook VBA and Custom Forms 4
C Required VBA code to complete task when replied to Outlook VBA and Custom Forms 2
Kelli VBA code for Outlook Using Outlook 1
S Requried a VBA Code to export Calander details to excel... Outlook VBA and Custom Forms 4
M VBA Code to extract data from an Outlook Form Using Outlook 0
M VBA code needed to move from Outlook 2010 subfolder to Symantec Vault subfolde Using Outlook 0
C In need of VBA code to read / parse HTML - Outlook emails Using Outlook 0
R [VBA] complicated(?) outlook events - need help with code Using Outlook 15
R how to get Outlook VBA code to work on the current folder Using Outlook 3
J VBA code can't be completely executed in outlook 2013 Using Outlook 0
D VBA: Send-From Code for Template Shortcut? Using Outlook 0
M Updating VBA code from outlook 2007 to outlook 2010 Using Outlook 1
J VBA code to determine 'show' state of SearchBar Using Outlook 7
D VBA code running on Server? Shared mailbox email routing Using Outlook 3
L Send E-mail with VBA code from [E-mail Distribution Group] if I have “Send as” Using Outlook 6
S Outllok 2007 VBA code to send mail automatically from drafts folder Using Outlook 1
S ThisOutlookSession in VBA not there anymore and code is gone Using Outlook 2
F No Attachment Warning - VBA Code HELP Outlook VBA and Custom Forms 1
J VBA code (rules) won't work after reboot Outlook VBA and Custom Forms 2
N VBA code to show today's date + a sequencial number in subject lin Outlook VBA and Custom Forms 1
A Accessing .pst folder in outlook using VBA Code Outlook VBA and Custom Forms 4
C Beginner Needs VBA Help in Modifying Code Outlook VBA and Custom Forms 2

Similar threads

Back
Top