Why does my VSTO Outlook Add-In hang on first run?
I开发者_C百科've written this little MS Outlook 2003 VSTO Add-In using C# and Visual Studio 2008. It is meant to check each Mail Item being sent for the word "attach" in the body and if found, then check the number of attachments. If that number is zero, then ask the user if they really mean to send the message. It's supposed to work like the Gmail labs feature which does the same thing.
The odd thing is that it works, but the first time I run it, I get a pause, like the mail item window is hung for about 45 seconds. Once it gets past that, it runs very fast for the rest of the time I have Outlook open. If I close Outlook though, then the next time I re-open it and send a message, I will have this wait again.
Any ideas, peoples?
Here's the code for my Add-In:
namespace OutlookAttacher
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is Microsoft.Office.Interop.Outlook.MailItem)
{
Microsoft.Office.Interop.Outlook.MailItem currentItem = Item as Microsoft.Office.Interop.Outlook.MailItem;
Cancel = true;
if (currentItem.Body.Contains("attach"))
{
if (currentItem.Attachments.Count > 0)
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
else
{
DialogResult ans = MessageBox.Show("This message has no attachments. Are you sure you want to send it?", "OutlookAttacher", MessageBoxButtons.YesNo);
if (ans.Equals(DialogResult.Yes))
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
}
}
else
{
Cancel = false;
//MessageBox.Show("This message will be sent now.");
currentItem.Send();
}
}
}
}
}
Any suggestions for improving the code are welcome as well, since this is my first stab at an Outlook Add-In.
Update: I am running this on a 5-year Dell laptop, 2 GB of Ram and I-don't-know-which Intel CPU. I like the idea of adding a trace / debugging it. I will have to go figure out how to step through the code so I can see where it might be taking the longest time. Thanks y'all!
OK, I'm really embarrassed. The delay I was seeing was just Outlook synching with my Exchange server. I was at home when I was testing and Outlook was still connecting via HTTP. I am seeing it work fast today, in the office, so no HTTP. Thanks for the replies anyway.
:-)
精彩评论