S
Sibi
Hello *,
several days before i ran into the error that my C# Addin (based on KB
Article : 302901) wouldn't let the process outlook.exe free. This only
happened with Outlook 2000, XP and 2003 worked fine. Therefore i've
started to crawl the newsgroups for any useful information. Well guess
what?! found nothing. So i started to debug the code. And yes! I found
the problem! The Microsoft guy who provided the sample forgot release
some ressources. I still don't know why it work under Outlook 2003.
Anyway, i would like to share this information with you! there you go:
in the method:
public void OnStartupComplete(ref System.Array custom)
//Add this line
Marshal.ReleaseComObject(oActiveExplorer);
//at the end of the method add following lines:
Marshal.ReleaseComObject(oStandardBar);
Marshal.ReleaseComObject(oCommandBars);
that solved the problem for me!
Full code listing:
public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oStandardBar;
try{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception){
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
//This line is needed
Marshal.ReleaseComObject(oActiveExplorer);
}
// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}
// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom
Button1"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button1";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}
MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
oStandardBar = null;
oCommandBars = null;
//Those lines are needed
Marshal.ReleaseComObject(oStandardBar);
Marshal.ReleaseComObject(oCommandBars);
}
Best Regards Sibi
several days before i ran into the error that my C# Addin (based on KB
Article : 302901) wouldn't let the process outlook.exe free. This only
happened with Outlook 2000, XP and 2003 worked fine. Therefore i've
started to crawl the newsgroups for any useful information. Well guess
what?! found nothing. So i started to debug the code. And yes! I found
the problem! The Microsoft guy who provided the sample forgot release
some ressources. I still don't know why it work under Outlook 2003.
Anyway, i would like to share this information with you! there you go:
in the method:
public void OnStartupComplete(ref System.Array custom)
//Add this line
Marshal.ReleaseComObject(oActiveExplorer);
//at the end of the method add following lines:
Marshal.ReleaseComObject(oStandardBar);
Marshal.ReleaseComObject(oCommandBars);
that solved the problem for me!
Full code listing:
public void OnStartupComplete(ref System.Array custom)
{
CommandBars oCommandBars;
CommandBar oStandardBar;
try{
oCommandBars =
(CommandBars)applicationObject.GetType().InvokeMember("CommandBars",
BindingFlags.GetProperty , null, applicationObject ,null);
}
catch(Exception){
// Outlook has the CommandBars collection on the Explorer object.
object oActiveExplorer;
oActiveExplorer=
applicationObject.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetProperty,null,applicationObject,null);
oCommandBars=
(CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",BindingFlags.GetProperty,null,oActiveExplorer,null);
//This line is needed
Marshal.ReleaseComObject(oActiveExplorer);
}
// Set up a custom button on the "Standard" commandbar.
try
{
oStandardBar = oCommandBars["Standard"];
}
catch(Exception)
{
// Access names its main toolbar Database.
oStandardBar = oCommandBars["Database"];
}
// In case the button was not deleted, use the exiting one.
try
{
MyButton = (CommandBarButton)oStandardBar.Controls["My Custom
Button1"];
}
catch(Exception)
{
object omissing = System.Reflection.Missing.Value ;
MyButton = (CommandBarButton) oStandardBar.Controls.Add(1, omissing ,
omissing , omissing , omissing);
MyButton.Caption = "My Custom Button1";
MyButton.Style = MsoButtonStyle.msoButtonCaption;
}
MyButton.Visible = true;
MyButton.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.MyButton_Click);
oStandardBar = null;
oCommandBars = null;
//Those lines are needed
Marshal.ReleaseComObject(oStandardBar);
Marshal.ReleaseComObject(oCommandBars);
}
Best Regards Sibi