开发者

Excel Interop - Getting type of Excel::Application::Selection

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"开发者_如何学JAVA method on the underlying type


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜