Outlook VSTO Custom task pane on Reading Pane & Compose window?

0

I'm trying to follow and combine 2 example projects from Microsoft:

https://docs.microsoft.com/en-us/visualstudio/vsto/walkthrough-synchronizing-a-custom-task-pane-with-a-ribbon-button?view=vs-2019

and

https://docs.microsoft.com/en-us/visualstudio/vsto/walkthrough-displaying-custom-task-panes-with-e-mail-messages-in-outlook?view=vs-2019

I want to have a custom tab on the main outlook window (Reading Pane) (explorer?) which works. And I want to have a taskpane with buttons that modify the current reply / forward email message. I also want this available when someone double clicks an email which brings up the pop-up compose window.

It all works great when going through the pop-up compose window. but it fails when going through the main Outlook Reading Pane (explorer??).

This is the error I recieve when trying to show / hide the taskpane on the main outlook window.

System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.Inspector'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063005-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

This is the line causing the exception

Outlook.Inspector inspector = (Outlook.Inspector)e.Control.Context;
c#
vsto
office-addins
asked on Stack Overflow Dec 4, 2020 by Adam Saunders

1 Answer

1

You need to cast e.Control.Context to Outlook.Explorer instead. Also, you might want to use the "as" operator - it does not raise an exception, and you can check the return value for null:

Outlook.Explorer explorer = e.Control.Context as Outlook.Explorer;
if (explorer != null)
{
    ...
}
answered on Stack Overflow Dec 4, 2020 by Dmitry Streblechenko • edited Dec 4, 2020 by Dmitry Streblechenko

User contributions licensed under CC BY-SA 3.0