Problem with attaching to reply button

J

Jessica Holtz

Hi all!

I'm hoping that someone can help me with a problem I'm having! :)

I'm trying to attach to the reply button in Outlook 2003 with C#, but I'm
getting an exception thrown everytime I try to access a commandbar, such as
"Standard"!

My code looks like:

private static Outlook.Application oApplication;
private static Microsoft.Office.Core.CommandBarButton cbExplReply;

.....
public void OnConnection(object application, Extensibility.ext_ConnectMode
connectMode, object addInInst, ref System.Array custom)
{
try
{
oApplication = (Outlook.Application)application;
Microsoft.Office.Core.CommandBar cbStandard =
(Microsoft.Office.Core.CommandBar)oApplication.ActiveExplorer().CommandBars[
"Standard"]; //Exception thrown on this line
cbExplReply =
(Microsoft.Office.Core.CommandBarButton)cbStandard.Controls["Reply"];
cbExplReply.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(OnToolbarBut
tonClick);
}catch( Exception ex )
{
System.Windows.Forms.MessageBox.Show( ex.ToString() );
}
}

private static void OnToolbarButtonClick(CommandBarButton cmdBarbutton,ref
bool cancel)
{

System.Windows.Forms.MessageBox.Show("Hello World","Jessica Addin");

}


This throws a:
System.NullReferenceException: Object reference not set to an instance of an
object.
at MyAddinJessica.Connect.OnConnection(Object application,
ext_ConnectMode connectMode, Object addInInst, Array& custom) in
c:\implementation\source_code\interface\connect.cs:line 178



Thanks Much!
Jessica
 
J

Jessica Holtz

I needed to use Reflection to bind to the CommandBars object in the
ActiveExplorer:

oActiveExplorer=
oApplication.GetType().InvokeMember("ActiveExplorer",BindingFlags.GetPropert
y,null,oApplication,null);
oCommandBars=
(Office.CommandBars)oActiveExplorer.GetType().InvokeMember("CommandBars",Bin
dingFlags.GetProperty,null,oActiveExplorer,null);
oStandardBar = oCommandBars["Standard"];
cbExplReply =
(Microsoft.Office.Core.CommandBarButton)oStandardBar.Controls["Reply"];
cbExplReply.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(OnToolbarBut
tonClick);

Does anyone know why I needed to use Reflection?

Thanks,
Jessica
 

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