如何检查选择/激活的形状/对象? [英] How to check which shapes/objects are selected/active?
问题描述
我想检查哪些对象被选中/处于活动状态.
I would like to check which objects are selected/active.
在PowerPoint和Word中,这非常容易,但是在Excel中...我检查的内容都没有用.Globals.ThisAddin.ActiveWindow.Selection的类型为:动态.我添加了对VisualBasic的引用,以便可以访问TypeName函数.
In PowerPoint and Word it is quite easy, but in Excel... nothing what I checked is working. Globals.ThisAddin.ActiveWindow.Selection is of type: dynamic. I added reference to VisualBasic, to have an access to TypeName function.
如果选择了图表,则返回的类型为"ChartObject" ...因此我将其设置为ChartObject类型的变量,但是后来我几乎无法访问其任何属性和方法,例如,当我尝试读取名称时该对象或试图从中返回图表给我一个错误.
If chart is selected it returns type "ChartObject"... so I am setting it to variable of ChartObject type, but then I have no access to almost any of its properties and methods, for example when I try to read name that object or trying to return chart from it gives me an error.
在有选择的则TypeName函数返回我键入几个形状:DrawingObjects" ......但我不能从中读什么.我试图从它的ShapeRange中获取信息,但是又一次出现了错误.
When there is few shapes selected then TypeName function returns me type: "DrawingObjects"... but I am not able to read anything from it. I was trying to get get from it ShapeRange, but again... errors.
您能建议我如何获取所有选定的对象吗?
Could you advise me how to get all selected objects?
推荐答案
我设法创建了适用于代码的代码(适用于图表):
I managed to create code that works (for charts):
public static List<XL.Chart> ReturnSelectedCharts(dynamic selection )
{
List<XL.Chart> charts=new List<XL.Chart>();
XL.ShapeRange selectedShapeRange = null;
XL.Chart chart=null;
try
{
selectedShapeRange = Globals.ThisAddIn.Application.Selection.ShapeRange;
for (int i = 1; i <= selectedShapeRange.Count; i++)
{
XL.Shape shape=selectedShapeRange.Item(i);
if (shape.HasChart==MsoTriState.msoTrue)
{
chart = shape.Chart;
charts.Add(chart);
}
}
}
catch
{
}
if (charts.Count==0)
{
try
{
chart = Globals.ThisAddIn.Application.ActiveChart;
charts.Add(chart);
}
catch (Exception)
{
}
}
return charts;
}
这篇关于如何检查选择/激活的形状/对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!