This project has moved and is read-only. For the latest updates, please go here.

Outlook IRibbonControl context - unable to cast to Inspector

Aug 26, 2013 at 9:09 PM
Edited Aug 26, 2013 at 9:18 PM
Hi all!
I just started to play with NetOffice and generally it looks excellent!
However I have bumped into a problem on my first project.
I have created a ribbon button in Outlook and want to manipulate the Mailitem object when the button is pressed. The event is working well and I get the IRibbonControl object in my function.
However, I am unable to cast the control.Context property.
 public void btnSaveSend_Click(NetOffice.OfficeApi.IRibbonControl control)
        var item = control.Context as NetOffice.OutlookApi.Inspector;
        var mailItem = item.CurrentItem as NetOffice.OutlookApi.MailItem;
        if (mailItem != null) MessageBox.Show(mailItem.Subject);
The above method only returns item = null. If I try this:
 var item = (NetOffice.OutlookApi.Inspector)control.Context;
I get an error message:
_Unable to cast COM object of type 'System._ComObject' to class type 'NetOffice.OutlookApi.Inspector'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
When debugging the add-in I can see that there is actually a MailItem object inside control.Context.CurrentItem, so this is all down to casting.

I have seen several code examples where this is supposed to work, but for me it doesn't.

Any help guiding me in the right direction is greatly appreciated!

Aug 26, 2013 at 9:59 PM
IRibbonControl is an earlybind interface in NetOffice. (necessary because the office application want this)
Outlook.Inspector item = new Outlook.Inspector(null, control.Context);
// stuff

Sep 17, 2013 at 8:59 AM
Hi Sebastian
I just noticed your reply as I was expecting an e-mail notification.

Your suggestion worked like a charm! Thank you!

Nov 14, 2013 at 6:37 AM
Had similar knowledge issue. Thanks!

Working on a word addin and needed to get the current document that is refreshing the editbox. When a control is invalidated and there are several documents open each window will get refreshed. Had the windows fighting over who ran last. The control.Context contains the window and subsequent document objects.

Spent a bunch of time examining the com object reference that I could never really get to. Could see the context in the debugger window dynamic view could render the object so I knew the object was obtainable just not reachable. Ended up coming back to read the documentation and trolling the issues:)

public string GetText_ebSomeEditboxControl(NetOffice.OfficeApi.IRibbonControl control)
        NetOffice.WordApi.Window window = new NetOffice.WordApi.Window(null, control.Context);

        var caption= window.Caption;


        return caption;