【问题标题】:What object type are multiple selected sheets in Excel?Excel中的多个选定工作表是什么对象类型?
【发布时间】:2012-05-14 21:36:09
【问题描述】:

与我的想法相反,多个选定的工作表不是 Worksheets 对象类型。

在多选 2 张或更多张之后。我得到选择的方式是这样的:

var selection = ExcelApp.Selection; //Returns object type

我已尝试获取“名称”属性以尝试通过使用后期绑定来给我一个提示:

string name = selection.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, selection, null).ToString();

但这会引发给定选择类型的错误。

有什么想法吗?

【问题讨论】:

  • selection.GetType().InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, selection, null) 似乎没有多大意义,因为您不能假设该对象具有 Name 属性。为什么不使用selection.GetType().Name
  • selection.GetType().Name 返回“_ComObject”,所以它不是很有用。我只是在黑暗中使用 Name 属性来获取对象类型,因为它适用于 Excel 中的大多数对象。我不打算在代码中实际使用它。
  • 好的,知道了。我看到一篇描述如何测试 COM 对象实现哪些接口的文章,但我想 Tim 的回答已经达到了目标。

标签: c# excel late-binding


【解决方案1】:

在 VBA 中,ActiveWindow.SelectedSheets 返回一个 Sheets 集合,所以我想它在 C# 中会类似

是的: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.window.selectedsheets(v=office.11).aspx

【讨论】:

  • + 1 :) 是的,在 C# 中也是如此 :)
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 2015-05-20
  • 1970-01-01
  • 2018-08-30
  • 2010-10-10
  • 2010-09-06
  • 2013-06-29
  • 1970-01-01
相关资源
最近更新 更多