del_ave

Hello,

I have created a simple web service (actually, it's the default one created by VS when selecting the "web service" item template). The service has one method:

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

I am using Visual Studio 2005, .NET 2.0 on an XP SP2 machine. The web service is deployed to the server (Windows 2003 Server, .NET 2.0, IIS 6.0) and I ensure that I can access the web service.

I have also created an Outlook 2003 add-in (VSTO 2005 SE, .NET 2.0 and C#). I added my web service as a "Web Reference" and VS added the appropriate maps and disco file(s).

I want to call my simple HelloWorld service as soon as an email is sent through Outlook. So, I write the following code for the add-in:

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Outlook.MAPIFolder o = this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

o.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_AddItem);

}

void SentItems_AddItem(object Item)

{

sws.SimpleWebService webs = new TestWeServiceCall.sws.SimpleWebService();

string strGreeting = webs.HelloWorld();

System.Threading.Thread.Sleep(100);

}

I put a breakpoint on the last line (System.Threading....) and test run my application by pressing the "F5" button on Visual Studio. Outlook 2003 is launched and everything seems ok.

I compose a new email message and send it out. The event fires as expected, calls the web service and return the "Hello World" string. The problem is that this works once only! Any attempts to send emails would not fire the SentItems_AddItem event and so the web service is not called.

I do not get any errors, exceptions or warnings. The web service is still available because if I close Outlook and restart the VSTO application, then I can send one email which will fire the SentItems_ItemAdd event.

Any ideas why this is happening and what can be done to achieve the desired functionality (call a web service upon sending an email from Outlook).

Thank you in advance.



Re: Visual Studio Tools for Office VSTO, Outlook and web services problem

Sue Mosher - Outlook MVP

As with all event handlers, the object raising the events needs to be declared at the class level, so that it doesn't go out of scope and get cleaned up by the .NET garbage collector.



Re: Visual Studio Tools for Office VSTO, Outlook and web services problem

del_ave

Thank you for your reply. I have tried your suggestion, but it did not solve the problem unfortunately. tried doing the following:

1. Move the folder declaration outside the Startup event handler:

Outlook.MAPIFolder o;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

o = this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

o.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_AddItem);

}

2. Move the folder declaration outside the Startup event handler and initialize it in the InternalSetup method generated by VSTO (although it says not to modify it with the code editor Stick out tongue) :

Outlook.MAPIFolder o;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

}

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

o = this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

o.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_AddItem);

}

3. Move the folder declaration outside the Startup event handler and initialize it in the the class' consturctor:

Outlook.MAPIFolder o;

public ThisAddIn()

{

o = this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

o.Items.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_AddItem);

}

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

}

In all three (3) cases, I made sure that the generated COM Add-in is removed before running the new code. I did that through Outlook's (2003) tools menu:

Tools -> Options -> "Other" tab -> "General Section" - Advanced Options -> COM Add-Ins... -> (remove all shown add-ins).

The problem persists. I am able to call the web service the first time only. I am not sure where else I should be declaring the objects/events. I am new to VSTO so I'm not 100% sure which other methods might be executing first.

Thanks again.





Re: Visual Studio Tools for Office VSTO, Outlook and web services problem

del_ave

I found the problem. I was doing something wrong as I suspected. Following Sue's advice, the following code would work just as I want it. The event fires and the web service is called everytime an email is sent.

Outlook.MAPIFolder o;

Outlook.Items oItems;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

o = this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderSentMail);

oItems = o.Items;

oItems.ItemAdd += new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_AddItem);

}

I was missing the explicit declaration of the Outlook.Items.

Thanks for the help Sue.





Re: Visual Studio Tools for Office VSTO, Outlook and web services problem

Sue Mosher - Outlook MVP

Glad you got it. It's worth noting that an explicit declaration of the Items collection is also required for such members as IncludeRecurrences and Sort.