Excel Interop - 获取 Excel::Application::Selection 的类型
当在 Excel 中选择某些内容时,Excel::Application::Selection 属性包含所选对象。例如,如果我选择某个单元格,我可以使用以下方法轻松地将它们转换为 Excel::Range:
Excel.Range cells = Excel.Application.Selection as Excel.Range
其中 Excel = Microsoft.Office.Interop.Excel
现在,当选择某些图片时,我必须将其转换为 Excel::Picture,然后是 Excel ::Shape 对于某些形状,但似乎每个形状都有不同的界面,如椭圆形、矩形等。我需要删除工作表上选择的任何内容。如果它是一个单元格,那么内容将被清除,图片、形状或 OLEObject 将被删除,但问题是我不想检查每个接口:
if (null != ThisApplication.Selection as Excel.Shape)
(ThisApplication.Selection as Excel.Shape).Delete();
else if (null != ThisApplication.Selection as Excel.Picture)
(ThisApplication.Selection as Excel.Picture).Delete();
else if (null != ThisApplication.Selection as Excel.OLEObject)
(ThisApplication.Selection as Excel.OLEObject).Delete();
我希望是否只有一个我可以访问的基本接口投射所有形状/图片并对其调用删除。
是否有可能获得:
- Application::Selection 内的真实类型 - 它显示 System::COMObject 但没有有关真实类型的信息 以
- 某种方式识别 Selection 包含图片/形状等,并在基础类型上调用“Delete”方法
When something is selected in Excel, the Excel::Application::Selection property contains the selected object. For e.g. if I select some cell, I can easily cast them to Excel::Range using:
Excel.Range cells = Excel.Application.Selection as Excel.Range
Where Excel = Microsoft.Office.Interop.Excel
Now when some picture is selected, I have to cast it to Excel::Picture, then Excel::Shape in case of some shapes but it seems there are different interfaces for each shape like Oval, Rectangle etc. I need to delete whatever thing is selected on the worksheet. If its a cell, then the contents will be cleared, a Picture,Shape or OLEObject will be deleted but the problem is that I do not want to check each and every interface:
if (null != ThisApplication.Selection as Excel.Shape)
(ThisApplication.Selection as Excel.Shape).Delete();
else if (null != ThisApplication.Selection as Excel.Picture)
(ThisApplication.Selection as Excel.Picture).Delete();
else if (null != ThisApplication.Selection as Excel.OLEObject)
(ThisApplication.Selection as Excel.OLEObject).Delete();
I wish if there is just one base interface to which I can cast all the Shapes/Pictures and call delete on them.
Is it possible to get:
- The real type inside Application::Selection - it displays a System::COMObject but no info on the real type
- Somehow identify that Selection contains a picture/shape etc and call the "Delete" method on the underlying type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我解决问题的方法。答案是使用VBA中的后期绑定。我们使用 Application.Run(...) 方法从 C# 插件中调用 VBA 宏。 VBA 宏只执行以下代码:
并且 VBA 对任何形状调用 Delete 方法。
This is how I solved my problem. The answer is to use the late binding in VBA. We call a VBA macro from within our C# addin using the Application.Run(...) method. The VBA macro just executes the following code:
and VBA calls the Delete method on whatever shape it is.