Results 1 to 8 of 8

Thread: VBA Code being completely ignored by Outlook

  1. #1
    paulricot is offline Junior Member Outlook 2010 64 bit
    Exchange Server account
    Join Date
    Feb 2013
    Posts
    4

    Default VBA Code being completely ignored by Outlook

    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!

  2. #2
    Diane Poremsky's Avatar
    Diane Poremsky is offline Outlook MVP Outlook 2013 64 bit
    Exchange Server account
    Join Date
    Jun 2009
    Location
    Ohio
    Posts
    4,542

    Default Re: VBA Code being completely ignored by Outlook

    Are macros enabled? File, Options, Trust Center, Macros. Set it to enable all to test.

  3. #3
    paulricot is offline Junior Member Outlook 2010 64 bit
    Exchange Server account
    Join Date
    Feb 2013
    Posts
    4

    Default Re: VBA Code being completely ignored by Outlook

    Quote Originally Posted by Diane Poremsky View Post
    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

  4. #4
    Diane Poremsky's Avatar
    Diane Poremsky is offline Outlook MVP Outlook 2013 64 bit
    Exchange Server account
    Join Date
    Jun 2009
    Location
    Ohio
    Posts
    4,542

    Default Re: VBA Code being completely ignored by Outlook

    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.

  5. #5
    Diane Poremsky's Avatar
    Diane Poremsky is offline Outlook MVP Outlook 2013 64 bit
    Exchange Server account
    Join Date
    Jun 2009
    Location
    Ohio
    Posts
    4,542

    Default Re: VBA Code being completely ignored by Outlook

    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

  6. #6
    paulricot is offline Junior Member Outlook 2010 64 bit
    Exchange Server account
    Join Date
    Feb 2013
    Posts
    4

    Default Re: VBA Code being completely ignored by Outlook

    Quote Originally Posted by Diane Poremsky View Post
    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

  7. #7
    Diane Poremsky's Avatar
    Diane Poremsky is offline Outlook MVP Outlook 2013 64 bit
    Exchange Server account
    Join Date
    Jun 2009
    Location
    Ohio
    Posts
    4,542

    Default Re: VBA Code being completely ignored by Outlook

    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

  8. #8
    paulricot is offline Junior Member Outlook 2010 64 bit
    Exchange Server account
    Join Date
    Feb 2013
    Posts
    4

    Default Re: VBA Code being completely ignored by Outlook

    Quote Originally Posted by Diane Poremsky View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •