2

如何找出从一些 .NET 函数返回的 COM 对象的属性和方法,这些似乎没有记录在案?

在我正在查看的特定示例中,我使用以下函数将图片插入 Excel:

Set NewPic = ActiveSheet.Pictures.Insert(FileName)

(请参阅此处的 SO 帖子。)

然而,这个函数的MSDN 文档只说 Worksheet.Pictures 返回一个对象,并且当我在调试期间监视变量时,它的类型是 System.__ComObject。我能否找出该类可能还有哪些其他属性和函数可用(例如,我想修改图片的替代文本)?发现这个功能的人怎么会Insert知道它?

MSDN 文档还倾向于说它们“不打算直接从您的代码中使用”这样的功能,但现在让我们忽略它......

谢谢!


编辑:好吧,我至少设法回答了我的具体问题。代替 using Worksheet.Pictures.Insert,您可以使用Worksheet.Shapes.AddPicture返回正确的(记录的) Excel.Shape 类:

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!"

不过,仍然会对未记录功能的任何资源感兴趣。

4

1 回答 1

7

找出这些东西的一种方法是使用该工具(您可以在此处OleView从 Microsoft 下载)。此工具允许您查看 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接口具有一个CutDelete方法,以及一个Item属性等。但是,请注意,此界面也标记为hidden,这(再次)很好地表明您不应该真正使用它(是的,我知道您忽略了该警告,但我赞同 Mitch Wheat 的评论,即这通常是一个坏主意使用隐藏类,因为它们通常是供应用程序内部使用的,如有更改,恕不另行通知。)

于 2009-03-27T04:29:52.767 回答