如何找出从某些.NET函数返回的COM对象的属性和方法,这些函数似乎没有记录?
在我正在查看的特定示例中,我正在使用以下函数将图片插入Excel:
Set NewPic = ActiveSheet.Pictures.Insert(FileName)
(请参阅此here上的SO帖子。)
但是,此函数的MSDN documentation仅表示Worksheet.Pictures返回一个Object,当我在调试期间对变量进行监视时,其类型为System .__ ComObject。我可以找出该类可用的其他属性和功能(例如,我想修改图片的替代文本)吗?发现Insert
功能的人怎么会知道它?
MSDN文档也倾向于说这些函数“它们不打算直接从你的代码中使用”,但是现在让我们忽略它......
谢谢!
编辑:好吧,我设法至少回答了我的具体问题。您可以使用Worksheet.Pictures.Insert
返回正确的(已记录的)Excel.Shape类,而不是使用Worksheet.Shapes.AddPicture
:
pic = range.Worksheet.Shapes.AddPicture(tmpFile, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, range.Left, range.Top, image.Width, image.Height)
pic.AlternativeText = "Help!"
但仍然对任何未记录的函数资源感兴趣。
答案 0 :(得分:7)
找到这些内容的一种方法是使用OleView
工具(可以从Microsoft here下载)。此工具允许您查看COM类型库。类型库(假设COM组件的供应商提供了类型库信息)包含有关接口以及应用程序或库公开的COM类的方法和属性的信息。
例如,在我的机器上,我可以查看C:\Program Files\Microsoft Office\Office12\EXCEL.EXE
的类型库,并查看Excel公开的COM对象及其属性和方法。 OleView
以IDL(接口描述语言)显示信息,这或多或少是一个带有额外属性的C函数原型。
这是我使用OleView
__Worksheet.Pictures
属性获得的IDL声明:
[id(0x00000303), hidden, helpcontext(0x00010303)]
HRESULT Pictures(
[in, optional] VARIANT Index,
[in, lcid] long lcid,
[out, retval] IDispatch** RHS);
请注意声明中的hidden
属性。这意味着它不会被大多数IDE显示(并且是一个很好的提示,不依赖于此方法始终存在 - Microsoft可以在更高版本的Excel中删除它。)
现在Pictures
班怎么样?这是完整的IDL:
[
uuid(000208A7-0000-0000-C000-000000000046),
helpcontext(0x00020067),
hidden
]
dispinterface Pictures {
properties:
methods:
[id(0x60000000), restricted]
void QueryInterface(
[in] GUID* riid,
[out] void** ppvObj);
[id(0x60000001), restricted]
unsigned long AddRef();
[id(0x60000002), restricted]
unsigned long Release();
[id(0x60010000), restricted]
void GetTypeInfoCount([out] unsigned int* pctinfo);
[id(0x60010001), restricted]
void GetTypeInfo(
[in] unsigned int itinfo,
[in] unsigned long lcid,
[out] void** pptinfo);
[id(0x60010002), restricted]
void GetIDsOfNames(
[in] GUID* riid,
[in] char** rgszNames,
[in] unsigned int cNames,
[in] unsigned long lcid,
[out] long* rgdispid);
[id(0x60010003), restricted]
void Invoke(
[in] long dispidMember,
[in] GUID* riid,
[in] unsigned long lcid,
[in] unsigned short wFlags,
[in] DISPPARAMS* pdispparams,
[out] VARIANT* pvarResult,
[out] EXCEPINFO* pexcepinfo,
[out] unsigned int* puArgErr);
[id(0x00000094), propget, helpcontext(0x00010094)]
Application* Application();
[id(0x00000095), propget, helpcontext(0x00010095)]
XlCreator Creator();
[id(0x00000096), propget, helpcontext(0x00010096)]
IDispatch* Parent();
[id(0x00010003), restricted, hidden]
void _Dummy3();
[id(0x0000025a), helpcontext(0x0001025a)]
VARIANT BringToFront();
[id(0x00000227), helpcontext(0x00010227)]
VARIANT Copy();
[id(0x000000d5), helpcontext(0x000100d5)]
VARIANT CopyPicture(
[in, optional, defaultvalue(2)] XlPictureAppearance Appearance,
[in, optional, defaultvalue(-4147)] XlCopyPictureFormat Format);
[id(0x00000235), helpcontext(0x00010235)]
VARIANT Cut();
[id(0x00000075), helpcontext(0x00010075)]
VARIANT Delete();
[id(0x0000040f), helpcontext(0x0001040f)]
IDispatch* Duplicate();
[id(0x00000258), propget, helpcontext(0x00010258)]
VARIANT_BOOL Enabled();
[id(0x00000258), propput, helpcontext(0x00010258)]
void Enabled([in] VARIANT_BOOL rhs);
[id(0x0000007b), propget, helpcontext(0x0001007b)]
double Height();
[id(0x0000007b), propput, helpcontext(0x0001007b)]
void Height([in] double rhs);
[id(0x0001000c), restricted, hidden]
void _Dummy12();
[id(0x0000007f), propget, helpcontext(0x0001007f)]
double Left();
[id(0x0000007f), propput, helpcontext(0x0001007f)]
void Left([in] double rhs);
[id(0x0000010d), propget, helpcontext(0x0001010d)]
VARIANT_BOOL Locked();
[id(0x0000010d), propput, helpcontext(0x0001010d)]
void Locked([in] VARIANT_BOOL rhs);
[id(0x0001000f), restricted, hidden]
void _Dummy15();
[id(0x00000254), propget, hidden, helpcontext(0x00010254)]
BSTR OnAction();
[id(0x00000254), propput, hidden, helpcontext(0x00010254)]
void OnAction([in] BSTR rhs);
[id(0x00000269), propget, helpcontext(0x00010269)]
VARIANT Placement();
[id(0x00000269), propput, helpcontext(0x00010269)]
void Placement([in] VARIANT rhs);
[id(0x0000026a), propget, helpcontext(0x0001026a)]
VARIANT_BOOL PrintObject();
[id(0x0000026a), propput, helpcontext(0x0001026a)]
void PrintObject([in] VARIANT_BOOL rhs);
[id(0x000000eb), helpcontext(0x000100eb)]
VARIANT Select([in, optional] VARIANT Replace);
[id(0x0000025d), helpcontext(0x0001025d)]
VARIANT SendToBack();
[id(0x0000007e), propget, helpcontext(0x0001007e)]
double Top();
[id(0x0000007e), propput, helpcontext(0x0001007e)]
void Top([in] double rhs);
[id(0x00010016), restricted, hidden]
void _Dummy22();
[id(0x0000022e), propget, helpcontext(0x0001022e)]
VARIANT_BOOL Visible();
[id(0x0000022e), propput, helpcontext(0x0001022e)]
void Visible([in] VARIANT_BOOL rhs);
[id(0x0000007a), propget, helpcontext(0x0001007a)]
double Width();
[id(0x0000007a), propput, helpcontext(0x0001007a)]
void Width([in] double rhs);
[id(0x0000026e), propget, helpcontext(0x0001026e)]
long ZOrder();
[id(0x000005f8), propget, helpcontext(0x000105f8)]
ShapeRange* ShapeRange();
[id(0x00000080), propget, helpcontext(0x00010080)]
Border* Border();
[id(0x00000081), propget, helpcontext(0x00010081)]
Interior* Interior();
[id(0x00000067), propget, helpcontext(0x00010067)]
VARIANT_BOOL Shadow();
[id(0x00000067), propput, helpcontext(0x00010067)]
void Shadow([in] VARIANT_BOOL rhs);
[id(0x00000105), propget, helpcontext(0x00010105)]
BSTR Formula();
[id(0x00000105), propput, helpcontext(0x00010105)]
void Formula([in] BSTR rhs);
[id(0x000000b5), helpcontext(0x000100b5)]
Picture* Add(
[in] double Left,
[in] double Top,
[in] double Width,
[in] double Height);
[id(0x00000076), propget, helpcontext(0x00010076)]
long Count();
[id(0x0000002e), helpcontext(0x0001002e)]
GroupObject* Group();
[id(0x000000fc), helpcontext(0x000100fc)]
Picture* Insert(
[in] BSTR Filename,
[in, optional] VARIANT Converter);
[id(0x000000aa), helpcontext(0x000100aa)]
IDispatch* Item([in] VARIANT Index);
[id(0xfffffffc), helpcontext(0x0000fffc)]
IUnknown* _NewEnum();
[id(0x000000d3), helpcontext(0x000100d3)]
Picture* Paste([in, optional] VARIANT Link);
据此,您可以推测Pictures
界面包含Cut
和Delete
方法,以及Item
属性等。但请注意,此界面也标记为hidden
,这(再次)是一个很好的迹象,表明您不应该真正使用它(是的,我知道您忽略了这个警告,但我是第二个Mitch Wheat的评论,使用隐藏类通常是一个坏主意,因为它们通常用于应用程序自己的内部使用,如有更改,恕不另行通知。)