1. Here's a thread that needs an answer: Blank Pop-up Window when click "Manage Sharing"

VBA Code being completely ignored by Outlook

Discussion in 'Using Outlook' started by paulricot, Feb 26, 2013.

  1. paulricot

    paulricot

    Member
    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. Diane Poremsky

    Diane Poremsky

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

     
  3. paulricot

    paulricot

    Member
    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. Diane Poremsky

    Diane Poremsky

    Senior Member
    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. Diane Poremsky

    Diane Poremsky

    Senior Member
    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. paulricot

    paulricot

    Member
    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. Diane Poremsky

    Diane Poremsky

    Senior Member
    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 (Text):
    Copy Source
    Private Sub Application_Startup()
     
    msgbox "Welcome"
     
    Dim objMyInbox As Outlook.MAPIFolder
     
    Set objNewMailItems = GetFolderPath("Forum Replies\Inbox").Items
     
    Set objMyInbox = Nothing
     
    End Sub


     
  8. paulricot

    paulricot

    Member
    Many thanks indeed for all your time and effort - I've now managed to get this working.

     
Similar Threads
Forum Title Date
Using Outlook Outlook 2010: Text in Body Being Change, HTML tags/code showing Dec 6, 2010
Using Outlook Outlook 2007 and 2010 emails being recieved with rtf/html code Jul 20, 2010
Outlook VBA and Custom Forms VBA code to save email attachments (PDF) as email subject line Aug 29, 2014
Outlook VBA and Custom Forms VBA Code to change subject Like Jul 18, 2014
Using Outlook Delete code and commandbutton at the same time. Jul 4, 2014

Share This Page