About Outlook addin events

A

Abhi.

I have problem in Outlook addin events this events just do occur
for first time only means once Item_Add event gets fired then after that no
event fires I think some where the events are washed out.
I have tried in InternalStartup() and as well in ThisApplication_Startup()
like , ldCalendar.Items.ItemAdd += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
All is working fine but due to events problem I am not getting to the
solution.
I think, somewhere Events should be raised from where should not
washed out. Should I do some extra for it ?

Thanks.
 
K

Ken Slovak - [MVP - Outlook]

Your event handlers are most likely being garbage collected. Declare class
level objects that will keep scope as long as your code is running and
attach the event handlers to those objects.

At class level:

Outlook.Items _items;

After instantiating _items as the Calendar.Items collection then add your
event handler.

When you post here please post the Outlook version and what language and
development platform you're using so people have sufficient information to
help.
 
K

Ken Slovak - [MVP - Outlook]

As I said, add an Items collection object at class level. Also, it's not
good practice to use a lot of dot operators as you're doing. .NET creates
internal object variables for each level of dot operator and you have no
control over those, when they go out of scope or if they create memory
leaks.

You also should be using the Outlook.Application object passed to you by
VSTO and not instantiating a new Outlook.Application object. The one passed
by VSTO is trusted, the one you create isn't trusted.

I'd really suggest that you download the sample VSTO Outlook addins from the
Office development Web site at MS to see the basics of how to do things and
what some of the best practices are.

What you need to do is something like this:

using Outlook = Microsoft.Office.Interop.Outlook;

public partial class ThisApplication
{

Outlook.MAPIFolder fldCalendar = null;

Outlook._Application outlookObj = null;

Outlook.Items _items = null;

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
outlookObj = this;

Outlook.NameSpace ns = outlookObj.GetNameSpace("MAPI);

fldCalendar =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

_items = (Outlook.Items)fldCalendar.Items;

_items.ItemChange += new
Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);

And so on.

That "_items" collection will remain in scope as long as your addin is
running and won't get garbage collected. You also see how I declare and use
separate Outlook objects instead of using multiple dot operators.
 
R

Roman Lonik

Hi,
I know it?s a 'little' bit old thread, but I have the exactly same problem. I have defined Items collection property on class level, registered ItemAdd handler but when I call appointment.Delete() method, that moves appointment from calendar to Deleted Items folder, event doesn?t fire.

It fires only when I delete it directly from Outlook as a user (Select appointment, right mouse click, Delete).

If there is another way, how to locate deleted item in Deleted Items folder, please let me know.
I have problem in Outlook addin events this events just do occur
for first time only means once Item_Add event gets fired then after that no
event fires I think some where the events are washed out.
I have tried in InternalStartup() and as well in ThisApplication_Startup()
like , ldCalendar.Items.ItemAdd += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
All is working fine but due to events problem I am not getting to the
solution.
I think, somewhere Events should be raised from where should not
washed out. Should I do some extra for it ?

Thanks.
On Tuesday, September 09, 2008 11:17 AM Ken Slovak - [MVP - Outlook] wrote:
Your event handlers are most likely being garbage collected. Declare class
level objects that will keep scope as long as your code is running and
attach the event handlers to those objects.

At class level:

Outlook.Items _items;

After instantiating _items as the Calendar.Items collection then add your
event handler.

When you post here please post the Outlook version and what language and
development platform you're using so people have sufficient information to
help.




news:[email protected]...
On Wednesday, September 10, 2008 8:09 AM Abh wrote:
Hello Ken,
Thanks for your reply.
I am using VS 2005 C# Office Outlook add-in for Outlook 2003. My code is
like,

public partial class ThisApplication
{

Microsoft.Office.Interop.Outlook.MAPIFolder fldCalendar = null;
Microsoft.Office.Interop.Outlook._Application outlookObj = new
Microsoft.Office.Interop.Outlook.Application();

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
fldCalendar =
(Microsoft.Office.Interop.Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
fldCalendar.Items.ItemChange += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);
fldCalendar.Items.ItemAdd += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);
fldCalendar.Items.ItemRemove += new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemRemoveEventHandler(Items_ItemRemove);


}


private void Items_ItemAdd(object Item)
{
}

private void Items_ItemChange(object Item)
{
}

private void Items_ItemRemove()
{
}

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);
}
}
even I tried with,

private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisApplication_Startup);
this.Shutdown += new
System.EventHandler(ThisApplication_Shutdown);

((Microsoft.Office.Interop.Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar)).Items.ItemAdd
+= new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

((Microsoft.Office.Interop.Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar)).Items.ItemChange
+= new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);

((Microsoft.Office.Interop.Outlook.MAPIFolder)outlookObj.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar)).Items.ItemRemove
+=new
Microsoft.Office.Interop.Outlook.ItemsEvents_ItemRemoveEventHandler(Items_ItemRemove);
}

So where should I add event handler now in order to avoid from being garbage
collected ?
Any other way to do this?
Thanks.

Abhi


"Ken Slovak - [MVP - Outlook]" wrote:
On Wednesday, September 10, 2008 9:23 AM Ken Slovak - [MVP - Outlook] wrote:
As I said, add an Items collection object at class level. Also, it's not
good practice to use a lot of dot operators as you're doing. .NET creates
internal object variables for each level of dot operator and you have no
control over those, when they go out of scope or if they create memory
leaks.

You also should be using the Outlook.Application object passed to you by
VSTO and not instantiating a new Outlook.Application object. The one passed
by VSTO is trusted, the one you create isn't trusted.

I'd really suggest that you download the sample VSTO Outlook addins from the
Office development Web site at MS to see the basics of how to do things and
what some of the best practices are.

What you need to do is something like this:

using Outlook = Microsoft.Office.Interop.Outlook;

public partial class ThisApplication
{

Outlook.MAPIFolder fldCalendar = null;

Outlook._Application outlookObj = null;

Outlook.Items _items = null;

private void ThisApplication_Startup(object sender, System.EventArgs e)
{
outlookObj = this;

Outlook.NameSpace ns = outlookObj.GetNameSpace("MAPI);

fldCalendar =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

_items = (Outlook.Items)fldCalendar.Items;

_items.ItemChange += new
Outlook.ItemsEvents_ItemChangeEventHandler(Items_ItemChange);

And so on.

That "_items" collection will remain in scope as long as your addin is
running and won't get garbage collected. You also see how I declare and use
separate Outlook objects instead of using multiple dot operators.




news:[email protected]...
 

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