Link to home
Start Free TrialLog in
Avatar of General_GSpot

asked on

Outlook VBA event triggering.

Does anyone know the syntax for opening a program when a new mail item is clicked on/opened? I want an outside program to launch only when I click on a new mailitem, and the rules aren't working the way they are supposed to.

It's Outlook 2007, by the way. I know I used to do something similar on Outlook 2000, but I understand MS may have changed it.

Avatar of David Lee
David Lee
Flag of United States of America image

Hi, General_GSpot.

Rules only run when an item is sent or received.  A rule cannot be triggered by clicking/opening an item.  Do you want the program to launch for every message you open or only certain ones?
Avatar of General_GSpot


I'd like to launch a program only when a new message is clicked on. Is that possible? I was trying to use a VB script with the .unread function to bring up a msgbox, for testing, but it didn't work. There was a brief pause, but no box.
Are you trying to trigger from a during a compose new email message event (the simple act of clicking the New E-mail button) or are you referring to new email (unread) from the Inbox?
Unread from the inbox. We have a program called Messagesave that launches when you send, I now want it to launch on reading of new emails in the inbox, Their tech support may add it in the future, but I'd love to have it now.

thanks in advance if you know a way...
Is it an external program (i.e. an EXE) or is it an Outlook add-in?  If the latter, then it may not be possible to do this.
It's an external, but it launches when the send button is hit, so I assume it could work this way.
Do you know that command to launch it?
Do you mean how it's launched now? If so, then no.
It's impossible to trigger it without knowing that.  I can provide code that traps the event that fires when a message opens and see if it's unread, but I'd have to know how to launch/open/activate Messagesave.
Thanks blue, actually, I couldn't even get a messagebox to fire, as a test. What is that code, if you don't mind? I have a call with the makers of the program later, and they know I want to do this, so I can ask them the exact launch code and/or switch. Supposedly, they are going to include this option in later releases, but I need to get it going sooner than 'summertime', as they say.
The code for this comes in two parts.  This is part #1.

Follow these instructions to add the code to Outlook.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  Right-click on Class Modules, select Insert > Class Module
5.  In the Properties panel click on Name and enter ExpWrapper
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor
Private WithEvents olkExp As Outlook.Explorer
Private WithEvents olkMsg As Outlook.MailItem
Private bolUnread As Boolean

Private Sub Class_Initialize()
    Set olkExp = Outlook.Application.ActiveExplorer
End Sub

Private Sub Class_Terminate()
    Set olkMsg = Nothing
    Set olkExp = Nothing
End Sub

Private Sub olkExp_SelectionChange()
    If olkExp.Selection.Count = 1 Then
        If olkExp.Selection(1).Class = olMail Then
            Set olkMsg = olkExp.Selection(1)
            bolUnread = olkMsg.UnRead
            Set olkMsg = Nothing
        End If
    End If
End Sub

Private Sub olkMsg_Open(Cancel As Boolean)
    If bolUnread Then
        'This is where the code goes that fires when a previously unread item is opened'
        MsgBox "You just opened an unread message"
    End If
End Sub

Open in new window

Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you sooo much. I've learned a great deal from this site.. I tell ya.

Have a great day.
Thanks and you're welcome.  Glad I could help.
Yeah, but bad news, the company says the product is a COM and I can't call it from VBA. I'm sure i'll be able to use the code for something in the future though. I'm going to get a book on VBA this weekend, but in the meantime, can you call a COM? It seems to me you should be able to.
Yes, you can call a COM object if you know it's details.  They'd have to tell you the class name to use and share information about its methods.
So probably no then huh? LOL. Well, I'm going to try a few things like the AutoHotKey program, and maybe some some sort of watcher with VB.

Anyway, don't get raptured, I need the help. Have a nice weekend.
"don't get raptured"

LOL!  I'm pretty sure I'm safe from that.

Thanks, you have a good one too.