On click event for mail item selection

M

Mark

I have a mail folder called MyInfo when I click on a mail item inside that
folder I want to execute vba code to go and grab details from a SQL Server
Database,
My problem is I cant find an "OnClick" event when a mail item is mouse clicked
Can any one help!
 
K

Ken Slovak - [MVP - Outlook]

When you click on an item in a folder it changes the selection. You can use
the Explorer.SelectionChange() event for what you want.

Instantiate an Explorer object declared WithEvents to handle the
SelectionChange() event and instantiate that Explorer object as the
ActiveExplorer.

When that event fires you can get the Explorer.Selection collection, which
can be iterated to handle all selected items. If there's only 1 selected
item then Selection.Item(1) will get that item in the event handler.
 
M

Mark

Im don't know how to perform the following:

"Instantiate an Explorer object declared WithEvents to handle the
SelectionChange() event and instantiate that Explorer object as the
ActiveExplorer."

Do you have an example!
_______________________________________________________________
 
K

Ken Slovak - [MVP - Outlook]

You can put the code in the ThisOutlookSession class and use something like
this:

Dim WithEvents oExpl As Outlook.Explorer

Private Sub Application_Startup()
Set oExpl = Application.ActiveExplorer
End Sub

Private Sub oExpl_SelectionChange()
Dim colSelection As Outlook.Selection
Dim obj As Object
Dim oMail As Outlook.MailItem

Set colSelection = oExpl.Selection
For Each obj In colSelection
If obj.Class = olMail Then
Set oMail = obj
' is a selected mail item do whatever with it
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top