如何使用应用程序级别VSTO插件删除Excel 2010中的ChartSheet?

时间:2015-02-04 09:43:31

标签: excel vsto

我有Excel 2010的应用程序级加载项。我需要能够从工作簿中删除工作表以及图表。使用以下代码删除工作表不是问题:

Worksheet wsDel = (Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets[WorksheetName];
wsDel.Delete();

然而,删除图表表是一个问题,因为我无法将它们转换为“ChartSheet”。宾语。我正在尝试以下代码:

object TheObject = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
for (int i = 1; i <= Globals.ThisAddIn.Application.ActiveWorkbook.Charts.Count; i++)
            {
                Debug.WriteLine("The type is '{0}'", Globals.ThisAddIn.Application.ActiveWorkbook.Charts[i]);
                object TempObject = Globals.ThisAddIn.Application.ActiveWorkbook.Charts[i];

                if (TheObject == TempObject)
                {
                    Debug.WriteLine("Eureka!");
                }
            }

&#39; TempObject&#39;属于&#39; __ ComObject&#39;。有什么想法,我可以把它删掉,所以我可以在Debug.WriteLine(&#34; Eureka&#34;)行删除它?

此致 Ĵ

1 个答案:

答案 0 :(得分:0)

首先,我建议打破调用链并在单独的代码行上声明每个属性或方法调用。因此,您将能够立即释放每个基础COM对象。完成使用后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Excel对象。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。您可以在MSDN的Systematically Releasing Objects文章中详细了解相关内容。它与Outlook有关,但相同的原则可以应用于所有Office应用程序。

您可以使用IDispatch和ITypeInfo接口来获取基础类型:

  1. 将对象强制转换为IDispatch类型。
  2. 通过IDispatch.GetTypeInfo()获取ITypeInfo接口。
  3. 使用ITypeInfo.GetDocumentation()获取类型名称。

    public static string GetTypeName(object comObj)
    {
    
        if (comObj == null)
            return String.Empty;
    
        if (!Marshal.IsComObject(comObj))
            //The specified object is not a COM object
            return String.Empty;
    
        IDispatch dispatch = comObj as IDispatch;
        if (dispatch == null)
            //The specified COM object doesn't support getting type information
            return String.Empty;
    
        ComTypes.ITypeInfo typeInfo = null;
        try
        {
            try
            {
                // obtain the ITypeInfo interface from the object
                dispatch.GetTypeInfo(0, 0, out typeInfo);
            }
            catch (Exception ex)
            {
                //Cannot get the ITypeInfo interface for the specified COM object
                return String.Empty;
            }
    
            string typeName = "";
            string documentation, helpFile;
            int helpContext = -1;
    
            try
            {
                //retrieves the documentation string for the specified type description 
                typeInfo.GetDocumentation(-1, out typeName, out documentation,
                    out helpContext, out helpFile);
            }
            catch (Exception ex)
            {
                // Cannot extract ITypeInfo information
                return String.Empty;
            }
            return typeName;
        }
        catch (Exception ex)
        {
            // Unexpected error
            return String.Empty;
        }
        finally
        {
            if (typeInfo != null) Marshal.ReleaseComObject(typeInfo);
        }
    }
    
  4. 有关详细信息,请参阅HowTo: get the actual type name behind System.__ComObject with Visual C# or VB.NET