Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试访问CustomTaskPane对象的属性时出现InvalidCastException_C#_Ms Word_Vsto_Office 2010 - Fatal编程技术网

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

C# 尝试访问CustomTaskPane对象的属性时出现InvalidCastException,c#,ms-word,vsto,office-2010,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

我在一台机器上遇到以下异常,而代码在所有其他机器上运行良好。此异常仅出现在一台客户机器上

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];
    }
公共静态Microsoft.Office.Tools.CustomTaskPane GetMyTaskPane(字符串docName=”“)
{
_Info(“输入GetMyTaskPane方法”);
docName=docName.ToLower();
_logger.Info(“docName:+docName”);
string docNameWithLivePreview=string.Empty;
如果(!string.IsNullOrEmpty(docName))
docNameWithLivePreview=docName.Substring(0,docName.LastIndexOf('.')>0?docName.LastIndexOf('.'):docName.Length-1)+
BusinessRule.LIVE\u预览\u文件\u后缀;
_logger.Info(“docNameWithLivePreview:+docNameWithLivePreview”);
_Info(“单词窗口计数:+Globals.TangoAddIn.Application.Windows.count”);
如果(Globals.TangoAddIn.Application.Windows.Count>0)
{
var win=Globals.TangoAddIn.Application.ActiveWindow;
_logger.Info(“活动窗口:+win.Caption”);
List customTaskPanes=新列表(Globals.tangoadin.customTaskPanes);
_logger.Info(“任务窗格总数:“+customTaskPanes.Count”);
foreach(customTaskPanes中的var customTaskPane)
{
尝试
{
_logger.Info(“TaskPane.Title:+customTaskPane.Title”);
_logger.Info(“TaskPane.Visible:+customTaskPane.Visible”);
_logger.Info(“TaskPane.Window.Caption”+customTaskPane.Window.Caption);
如果(!string.IsNullOrEmpty(docName))
{
_logger.Info(“根据docName:+docName获取任务窗格”);
如果(((窗口)customTaskPane.Window).Document.FullName.ToLower().Equals(docName)||
((窗口)customTaskPane.Window.Document.FullName.ToLower().Equals(docNameWithLivePreview))
返回自定义任务窗格;
其他的
_logger.Info(“根据docName未找到任务窗格”);
}
如果((窗口)customTaskPane.Window==win),则为else
{
_Info(“基于窗口获取任务窗格”);
返回自定义任务窗格;
}
_logger.Info(“任务窗格:+customTaskPane.Title+“不需要”);
}
捕获(例外情况除外)
{
_logger.Info(“获取任务窗格时出现异常”+ex);
Globals.tangoadIn.CustomTaskPanes.Remove(customTaskPane);
}
_logger.Info(“重试…”);
}
}
其他的
{
_logger.Info(“窗口数量为0,清理任务窗格集合”);
//表示只有一个Word实例,因此请清理taskpane集合
Driver.cleanstaskPaneCollection();
}
_logger.Error(“未找到CTP。返回null”);
if(Globals.TangoAddIn.Application.Documents.Count==0)
{
_logger.Info(“返回任务窗格@索引0”);
返回Globals.tangoadIn.CustomTaskPanes[0];
}
其他的
{
_Info(“无法找到任务窗格,因此不返回任何内容”);
返回null;
}
//返回Globals.tangoadIn.CustomTaskPanes[0];
}
在以下行中使用任务窗格

List<CustomTaskPane> customTaskPanes = new List<CustomTaskPane>(Globals.TangoAddIn.CustomTaskPanes);
List customTaskPanes=新列表(Globals.tangoadin.customTaskPanes);
是Microsoft.Office.Tools.CustomTaskPane(当我将鼠标悬停在VS中的“CustomTaskPane”上时,将显示此路径)

有人能告诉我为什么.NET/Office试图将对象强制转换为“Microsoft.Office.Core.\u CustomTaskPane”,然后引发无效强制转换异常。此外,为什么这是特定于机器的

我已尝试修复office安装,但未能解决错误。客户机器上还安装了VSTO 2010(V10.0.60724)和office professional plus 2010(V14.0.7015.1000)


致以最诚挚的问候,

您能发布一些与
TangoadIn
相关的代码吗?建议该问题可能是由于另一个线程造成的。我想TangoadIn不会有太大帮助,但我发现一件事,当我从Task.ContinueWith()调用GetMyTaskPane()方法时,会发生此异常,默认情况下,任务的单元状态为MTA。但让我困惑的是,为什么这只发生在一台机器上,而在其他机器上,它工作得非常好。这件事发生时没有任何变化。我的意思是,客户没有更改与我的外接程序相关的任何文件,而且这也没有在我的开发或测试环境中进行复制