尝试访问CustomTaskPane对象的属性时出现InvalidCastException

时间:2017-07-21 05:57:20

标签: c# ms-word vsto office-2010

我在一台计算机上遇到异常,而代码在所有其他计算机上运行正常。此例外仅发生在一台客户机器上

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)

以下是代码:

    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];
    }

任务窗格用于以下行

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

是Microsoft.Office.Tools.CustomTaskPane(当我将鼠标悬停在&#34; CustomTaskPane&#34;在VS中时,会显示此路径。)

有人可以帮助我为什么.NET / Office试图将对象转换为&#34; Microsoft.Office.Core._CustomTaskPane&#34;然后抛出无效的强制转换异常。此外,为什么这是机器特定的?

我已经尝试修复办公室安装,但这并没有解决错误。此外,VSTO 2010(v 10.0.60724)和office professional plus 2010(v 14.0.7015.1000)也安装在客户机器上。

最诚挚的问候,

0 个答案:

没有答案