We help IT Professionals succeed at work.

Outlook VBA event triggering.

1,128 Views
Last Modified: 2012-05-11
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.

Thanks
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
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?

Author

Commented:
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.
Top Expert 2011

Commented:
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?

Author

Commented:
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...
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
It's an external, but it launches when the send button is hit, so I assume it could work this way.
CERTIFIED EXPERT
Top Expert 2010

Commented:
Do you know that command to launch it?

Author

Commented:
Do you mean how it's launched now? If so, then no.
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
    olkExp_SelectionChange
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
        Else
            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

CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you sooo much. I've learned a great deal from this site.. I tell ya.

Have a great day.
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thanks and you're welcome.  Glad I could help.

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2010

Commented:
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.

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2010

Commented:
"don't get raptured"

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

Thanks, you have a good one too.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.