Excel Interop - 获取 Excel::Application::Selection 的类型

发布于 2024-10-07 08:27:40 字数 1004 浏览 5 评论 0原文

当在 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();

我希望是否只有一个我可以访问的基本接口投射所有形状/图片并对其调用删除。

是否有可能获得:

  1. Application::Selection 内的真实类型 - 它显示 System::COMObject 但没有有关真实类型的信息 以
  2. 某种方式识别 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:

  1. The real type inside Application::Selection - it displays a System::COMObject but no info on the real type
  2. Somehow identify that Selection contains a picture/shape etc and call the "Delete" method on the underlying type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-10-14 08:27:40

这就是我解决问题的方法。答案是使用VBA中的后期绑定。我们使用 Application.Run(...) 方法从 C# 插件中调用 VBA 宏。 VBA 宏只执行以下代码:

Application.Selection.Delete  

并且 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:

Application.Selection.Delete  

and VBA calls the Delete method on whatever shape it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文