如何将用户的选择转换为VSTO外接程序中的图表对象(类似于Excel.Chart)。
我一直试图使用这样的方法(同时在Excel中选择图表对象):
Dim chart as Excel.Chart = CType(Globals.ThisAddIn.Application.Selection, Excel.Chart)
但是,这会引发一个InvalidCastException。
我似乎找不到任何文档,说明如何允许用户选择图表,然后在VSTO外接程序中修改选定的图表。
发布于 2016-04-30 18:07:06
您的代码需要确定所选内容是否实际包含图表对象,然后从该对象派生图表。为了确定所选内容所包含的内容,在使用VB.NET时使用返回字符串的TypeName方法,然后计算字符串:
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 Functionhttps://stackoverflow.com/questions/36925352
复制相似问题