SelectionChange Event

A

A. Blundon

Hi folks,

I have a quick (yet probably easy question). I've done some VBA in
excel for the generation of engineering calculation spreadsheets but
this is my first kick at VBA in Outlook.

I'm trying to create a routine where I can click a message in my Inbox
(or elsewhere) and save this message to a msg file in a specific
locaiton. The file format and path for the save will be based on the
contents of the email subject, the send date, who it was from, etc.

I have this part of the routine working fine. I click a message, then
click the menu button, my form pops up it automatically fills in the
fields for date, subject etc. I can then click a save button and the
message will be saved to a msg file with the appropriate filename. The
next thing I would like to do is be able to leave the form open and
switch to a different message. When I switch, I'd like the date,
subject, sender info to be updated with the new message selected. From
what I can tell I need to use a selectionchange event to trap this.

I've never really worked with class modules before. From what I can
tell I have to create a new class module. I found this code on the net
and added it to the class module:

Dim WithEvents myolexp As Outlook.Explorer

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

Private Sub myOlExp_Close()
' Need to do this so Outlook exits properly.
Set myolexp = Nothing
End Sub

Private Sub myOlExp_SelectionChange()
MsgBox "SelectionChange"
frmSaveMessage.lbltest.Caption = "Changed"
End Sub

When I add this code to ThisOutlookSession, the event triggers
everytime. I want it so that the event only triggers when I have my
form opened.

HELP!

Thanks,
AB
 
E

Eric Legault [MVP - Outlook]

When dealing with Outlook items in their own windows, it's best to trap the
Inspectors collection which gets populated by each window that opens. See my
blog for an example:

Getting a Handle on Your E-mails with VBA:
http://blogs.officezealot.com/legault/articles/2224.aspx

Taking it even further is this sample code:
http://www.slovaktech.com/code_samples.htm#InspectorWrapper

This allows you to have separate "threads" for each item that opens, so you
can trap events for them individually. This concept can be a little tough to
consume, but here's an excellent sample project that you can examine and even
run to help illustrate it:

http://www.microeye.com/resources/itemsCB.htm
 
A

A. Blundon

Thanks for the reply.

Your reply seems to deal with outlook items that have been opened (i.e.
by double clicking an email and opening the viewer).

In my case, it is simply selecting the messages in the inbox (without
viewing the message contents).

As part of my routine I've used the following code to extract the
various parts of the message I need:

Set objExplorer = objApplication.ActiveExplorer
Set objSelection = objExplorer.Selection
Set obj1stSelectedMessage = objSelection.Item(1)

That way, i'll be processing the first selected item.

AB
 

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