InvalidCastException when trying to access property of CustomTaskPane object

0

I'm getting following exception on a one single machine whereas the code runs fine on all other machine. This exception is only coming on one customer machine

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

at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
   at Microsoft.Office.Core._CustomTaskPane.get_Title()
   at Microsoft.Office.Tools.CustomTaskPaneImpl.get_Title()
   at HeliumRtfEditorAddIn.Driver.GetMyTaskPane(String docName)

And here is the code:

    public static Microsoft.Office.Tools.CustomTaskPane GetMyTaskPane(string docName = "")
    {
        _logger.Info("Entering GetMyTaskPane method");

        docName = docName.ToLower();

        _logger.Info("docName: " + docName);

        string docNameWithLivePreview = string.Empty;

        if (!string.IsNullOrEmpty(docName))
            docNameWithLivePreview = docName.Substring(0, docName.LastIndexOf('.') > 0 ? docName.LastIndexOf('.') : docName.Length - 1) +
                BusinessRule.LIVE_PREVIEW_FILE_POSTFIX;

        _logger.Info("docNameWithLivePreview: " + docNameWithLivePreview);

        _logger.Info("Word Window count: " + Globals.TangoAddIn.Application.Windows.Count);

        if (Globals.TangoAddIn.Application.Windows.Count > 0)
        {
            var win = Globals.TangoAddIn.Application.ActiveWindow;

            _logger.Info("active window: " + win.Caption);

            List<CustomTaskPane> customTaskPanes = new List<CustomTaskPane>(Globals.TangoAddIn.CustomTaskPanes);
            _logger.Info("Total taskpanes: " + customTaskPanes.Count);

            foreach (var customTaskPane in customTaskPanes)
            {
                try
                {
                    _logger.Info("TaskPane.Title: " + customTaskPane.Title);
                    _logger.Info("TaskPane.Visible: " + customTaskPane.Visible);
                    _logger.Info("TaskPane.Window.Caption" + customTaskPane.Window.Caption);

                    if (!string.IsNullOrEmpty(docName))
                    {
                        _logger.Info("Getting taskpane on the basis of docName: " + docName);
                        if (((Window)customTaskPane.Window).Document.FullName.ToLower().Equals(docName) ||
                            ((Window)customTaskPane.Window).Document.FullName.ToLower().Equals(docNameWithLivePreview))
                            return customTaskPane;
                        else
                            _logger.Info("taskpane not found on the basis of docName");
                    }
                    else if ((Window)customTaskPane.Window == win)
                    {
                        _logger.Info("Getting taskpane based on Window");
                        return customTaskPane;
                    }

                    _logger.Info("Taskpane: " + customTaskPane.Title + "not required");
                }
                catch (Exception ex)
                {
                    _logger.Info("Exception while getting taskpane " + ex);
                    Globals.TangoAddIn.CustomTaskPanes.Remove(customTaskPane);
                }

                _logger.Info("retrying...");
            }
        }
        else
        {
            _logger.Info("No of windows is 0, cleaning taskpane collection");
            //Means there is only one instance of Word so clean the taskpane collection
            Driver.CleanTaskPaneCollection();
        }
        _logger.Error("CTP not found. Returning null");

        if (Globals.TangoAddIn.Application.Documents.Count == 0)
        {
            _logger.Info("Returning taskpane @ index 0");

            return Globals.TangoAddIn.CustomTaskPanes[0];
        }
        else
        {
            _logger.Info("Unable to find taskpane, therefore, returning nothing");
            return null;
        }
        //return Globals.TangoAddIn.CustomTaskPanes[0];
    }

Taskpane used in following line

List<CustomTaskPane> customTaskPanes = new List<CustomTaskPane>(Globals.TangoAddIn.CustomTaskPanes);

is Microsoft.Office.Tools.CustomTaskPane (When I hover over the "CustomTaskPane" in VS, this path is displayed).

Can someone help me why the .NET/Office is trying to cast the object to "Microsoft.Office.Core._CustomTaskPane" and then throwing invalid cast exception. Besides, why this is machine specific?

I've tried to repair the office installation but this didn't resolve the error. Also VSTO 2010 (v 10.0.60724) and office professional plus 2010 (v 14.0.7015.1000) are installed on customer machine.

Best Regards,

c#
ms-word
vsto
office-2010
asked on Stack Overflow Jul 21, 2017 by phantom • edited Jul 21, 2017 by YowE3K

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0