VSTO 中实现Selection对象中获取ChartObjects 、ShapeRange 源码
时间: 2024-03-10 16:45:10
浏览: 37
在 VSTO 项目中,您可以使用 `Globals.ThisAddIn.Application.Selection` 获取当前选定的[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083),然后根据选定的[对象](https://geek.csdn.net/educolumn/04c51611e4b730957464192e0307b82c?spm=1055.2569.3001.10083)类型进行类型转换。以下是获取选定的 `ChartObjects` 和 `ShapeRange` 的示例代码:
using Excel = Microsoft.Office.Interop.Excel;
// 获取选定的对象
object selection = Globals.ThisAddIn.Application.Selection;
// 判断选定的对象是否为图表
if (selection is Excel.ChartObjects)
// 获取选定的图表对象[集合](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)
Excel.ChartObjects selectedCharts = (Excel.ChartObjects)selection;
// 遍历选定的图表对象[集合](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)
foreach (Excel.ChartObject chartObject in selectedCharts)
// 获取图表对象
Excel.Chart chart = chartObject.Chart;
// 处理每个图表对象
// ...
// 判断选定的对象是否为形状
if (selection is Excel.ShapeRange)
// 获取选定的形状对象[集合](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)
Excel.ShapeRange selecte
```