SelectionChange event in Outlook 2007 calendar

M

Marcin

Hi,
I am writing an add-in, that does few things, with emails and appointments
on the calendar. I need to read every item once it it selected by user,
since ItemLoad fires only once, and I got to get Item's few properties upon
it's selection.
Handling MailItems this way works just fine, but I encountered strange
behaviour. Switching to calendar, and selecting any existing appointment,
makes SelectionChange event be triggered multiple times, instead of just one.
Any idea why ?
My event code :
void ThisAddIn_SelectionChange()
{
try
{
if (Application.ActiveExplorer().Selection.Count > 0)
{
if (Application.ActiveExplorer().Selection[1] is
Outlook.MailItem)
{
Outlook.MailItem Item =
(Outlook.MailItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.MailItem)Item;
}
if (Application.ActiveExplorer().Selection[1] is
Outlook.AppointmentItem)
{
Outlook.AppointmentItem Item =
(Outlook.AppointmentItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.AppointmentItem)Item;
}
if (Application.ActiveExplorer().Selection[1] is
Outlook.MeetingItem)
{
Outlook.MeetingItem Item =
(Outlook.MeetingItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.MeetingItem)Item;
}
// set certain properties for the add-in, no matter what
type of Item is being dealt with
this.setProperties();
}
}
catch (Exception ex)
{
//do nothing yet
}
}

The .setProperties() does same kind of check that is :
 
K

Ken Slovak - [MVP - Outlook]

When switching a folder you are changing Selection since the
ActiveExplorer().CurrentFolder object is changing. Then when you select one
or more items Selection will change again. Is that what you're seeing?
 
M

Marcin

No, that's no what I meant. That would be a normal behaviour.
What I mean is following.
1. open outlook, by default it selects first email in the default folder
2. switch to calendar, with breakpoints on the beginning, and every if
clause of SelectionChange event.
3. it continuously enters the event handler, recognises _lack_ of any
selection, exists first if clause without any action, re-enters event handler
and so on. Exit to Outlook is only possible with disabling breakpoints.
Re-enabling them, and selecting any existing item on the calendar (item, not
empty date), causes exactly same behaviour. Since exception handling is in
place ... and no exception gets thrown unhandled, it seems a bit weird to me.


Regards

MArcin

Ken Slovak - said:
When switching a folder you are changing Selection since the
ActiveExplorer().CurrentFolder object is changing. Then when you select one
or more items Selection will change again. Is that what you're seeing?




Marcin said:
Hi,
I am writing an add-in, that does few things, with emails and appointments
on the calendar. I need to read every item once it it selected by user,
since ItemLoad fires only once, and I got to get Item's few properties
upon
it's selection.
Handling MailItems this way works just fine, but I encountered strange
behaviour. Switching to calendar, and selecting any existing appointment,
makes SelectionChange event be triggered multiple times, instead of just
one.
Any idea why ?
My event code :
void ThisAddIn_SelectionChange()
{
try
{
if (Application.ActiveExplorer().Selection.Count > 0)
{
if (Application.ActiveExplorer().Selection[1] is
Outlook.MailItem)
{
Outlook.MailItem Item =
(Outlook.MailItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.MailItem)Item;
}
if (Application.ActiveExplorer().Selection[1] is
Outlook.AppointmentItem)
{
Outlook.AppointmentItem Item =
(Outlook.AppointmentItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.AppointmentItem)Item;
}
if (Application.ActiveExplorer().Selection[1] is
Outlook.MeetingItem)
{
Outlook.MeetingItem Item =
(Outlook.MeetingItem)Application.ActiveExplorer().Selection[1];
this.OutlookItem = (Outlook.MeetingItem)Item;
}
// set certain properties for the add-in, no matter
what
type of Item is being dealt with
this.setProperties();
}
}
catch (Exception ex)
{
//do nothing yet
}
}

The .setProperties() does same kind of check that is :
 
K

Ken Slovak - [MVP - Outlook]

Does it do the same thing if instead of breakpoints you put
Debug.WriteLine() or MessageBox.Show() statements?
 
M

Marcin

Yes it does, at least with MessageBoxes. Haven't tried yet with printing
debug, but will do so soon and will post results.
 
M

Marcin

With Debug.WriteLine() it seems to be handling it only once, as it should be
done. Since all the exceptions are handled ... seems I need to get back to
debugging :-(

Had a deeper look at SelectionChange event ... what I noticed is that it
fires everytime I switch between applications, which messes up my application
logic then ...

Thanks Ken

marcin
 

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