Avatar of General_GSpot
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.

Thanks
OutlookEmail ClientsVB Script

Avatar of undefined
Last Comment
David Lee

8/22/2022 - Mon
David Lee

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?
General_GSpot

ASKER
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.
yobri

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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
General_GSpot

ASKER
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...
David Lee

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.
General_GSpot

ASKER
It's an external, but it launches when the send button is hit, so I assume it could work this way.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Lee

Do you know that command to launch it?
General_GSpot

ASKER
Do you mean how it's launched now? If so, then no.
David Lee

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
General_GSpot

ASKER
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.
David Lee

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

ASKER CERTIFIED SOLUTION
David Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
General_GSpot

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

Have a great day.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Lee

Thanks and you're welcome.  Glad I could help.
General_GSpot

ASKER
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.
David Lee

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
General_GSpot

ASKER
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.
David Lee

"don't get raptured"

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

Thanks, you have a good one too.