gnirts_modnar

Hello everybody,

I'm trying to build a VSTO add in that shows a dialog through adding a button to the command bar of a mail item. However i've encountered the strangest problem. When i debug my app it shows the dialog correctly the first time i press the button. It even handles all the code i've written without a problem. However, after the dialog is closed, when i try to push the button again, it doesn't work anymore. I stripped my code completely down to find the problem and i came to the conclusion it had to do something with the ShowDialog() method.

Code Snippet

public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Outlook.Inspectors insp = this.Application.Inspectors;
insp.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(insp_NewInspector);
}
void insp_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
if (Inspector.CurrentItem is Outlook.MailItem)
{
Office.CommandBarButton btn;

Office.CommandBar cbar = Inspector.CommandBars.ActiveMenuBar;
btn = (Office.CommandBarButton)cbar.Controls.Add(Office.MsoControlType.msoControlButton,missing,missing,missing,true);
btn.Caption = "Artikelen toevoegen in email";
btn.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;
btn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btn_Click);
}
}





void btn_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
{

ListBox.ObjectCollection values;

Form2 daniel = new Form2();
if (daniel.ShowDialog() == DialogResult.OK){
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }

}



It doesn't show any errors.
Can someone please explain to me what the problem is.

Kind regards,




Re: Visual Studio Tools for Office VSTO Outlook 2007 showDialog() problem

Andrew Cherry [MSFT]

Hi -

It looks like you might be suffering from an overoptimization of garbage collection. There are issues, particularly with Outlook, where events are lost due to the object they are connected to being garbage collected.

I would suggest scoping your Office.CommandBarButton outside your NewInspector event, and making it a class variable. Similar issues have been resolved in such a way.

HTH,

Andrew






Re: Visual Studio Tools for Office VSTO Outlook 2007 showDialog() problem

gnirts_modnar

Well it seems that did the trick. Thank you very much for the quick response.

Is this problem occurring only in this particular situation or are there some things i should bear in mind when creating other applications.

Thanks!