首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel VSTO AddIn -从选择中获取图表对象

Excel VSTO AddIn -从选择中获取图表对象
EN

Stack Overflow用户
提问于 2016-04-28 20:59:02
回答 1查看 706关注 0票数 0

如何将用户的选择转换为VSTO外接程序中的图表对象(类似于Excel.Chart)。

我一直试图使用这样的方法(同时在Excel中选择图表对象):

Dim chart as Excel.Chart = CType(Globals.ThisAddIn.Application.Selection, Excel.Chart)

但是,这会引发一个InvalidCastException

我似乎找不到任何文档,说明如何允许用户选择图表,然后在VSTO外接程序中修改选定的图表。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-30 18:07:06

您的代码需要确定所选内容是否实际包含图表对象,然后从该对象派生图表。为了确定所选内容所包含的内容,在使用VB.NET时使用返回字符串的TypeName方法,然后计算字符串:

代码语言:javascript
复制
Public Function IsSelectionChart() as Boolean
    Dim cht As Excel.Chart
    Dim sSelectionType As String
    Dim xlApp As Excel.Application = Globals.ThisAddIn.Application

    sSelectionType = TypeName(xlApp.Selection)
    Select Case sSelectionType
        Case "ChartArea", "PlotArea", "Legend"
            cht = xlApp.Selection.Parent
            Debug.Print(cht.Name)
        Case "Series"
            cht = xlApp.Selection.Parent.Parent
            Debug.Print(cht.Name)
        Case "Axis" 'Parent is Worksheet
            Debug.Print("Can't determine the chart from an Axis")
        Case Else
            Debug.Print("Not a chart-related object")
    End Select

    If Not cht Is Nothing Then 
       Return True
    Else
       Return False
    End If
End Function
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36925352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档