首页 > 解决方案 > 使用 C# VSTO 插件获取选定的图表对象或文本框或表格?

问题描述

我试图为 VSTO 插件获取 PPTX 文件中存在的当前选定对象。

我使用下面的包在幻灯片中创建图表、表格和文本。

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;

我在 Ribben 栏中有一个按钮。一旦用户单击它,我想在任何幻灯片中获取当前选定的对象。我该怎么做呢?interop包里有没有办法。

标签: c#vstointeroppowerpoint-interop

解决方案


如果您选择一个或多个形状,您可以执行以下操作

if (Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange.Count > 0)
                {
                    //iterate over all the shapes selected by the user
                    foreach (PowerPoint.Shape shp in Globals.ThisAddIn.Application.ActiveWindow.Selection.ShapeRange)
                    {
                        if (shp.HasTable == Office.MsoTriState.msoTrue)
                        {
                            //Do something
                        }
                        if (shp.HasChart == Office.MsoTriState.msoTrue)
                        {
                            //Do something
                        }
                        if (shp.HasTextFrame == Office.MsoTriState.msoTrue)
                        {
                            //Do something
                        }

                        //or you could just return the shp object
                    }
                }

推荐阅读