C#和OLEFormat.Object? [英] C# and OLEFormat.Object?

查看:119
本文介绍了C#和OLEFormat.Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#add in将自定义活动x控件

插入到工作表中。


插入对象后我可以找到对象

通过Shapes和Worksheet.OLEObjects。我不能做什么就是弄清楚如何以编程方式使用对象




我添加了一个引用通过导入对象OCX,Visual Studio

项目中的对象。参考显示

up作为Interop.ActiveXLib和我的对象方法以及

属性在对象浏览器中显示在

interop.ActiveXLib下。 ImyControl和

interop.ActiveXLib.myControlCtlClass。


我已经尝试过各种投射物体的安排

从两个形状返回工作表的OLEFormat.Object

试图得到一些我可以直接使用的东西没有

成功:


ActiveXLib。 myControlCtlClass mc =

(ActiveXLib.myControlCtlClass)shape.OLEFormat.Object;


我在Visual Studio .Net中编写添加内容它是

在Excel 2002 Service Pack 1中运行。


我想要做的是能够使用:

mc.myControlMethod(doIT )。


感谢您的帮助


Mark

解决方案

< blockquote>

嗨Mark,


感谢您发布此群组p。

将shape.OLEFormat.Object转换为

ActiveXLib.myControlCtlClass时的错误信息是什么?

我想你可以使用typeof运算符来确定

shape.OLEFormat.Object的类型。

也许您可以尝试使用ActiveXLib.ImyControl来获取此接口。

你确定这个对象是ActiveXLib.myControlCtlClass类型吗?


祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


--------------------

| Content-Class:urn:content-classes:message

|来自:马克 < mh*@yadayada.com>

|发件人:标记 < mh*@yadayada.com>

|主题:C#和OLEFormat.Object?

|日期:2003年10月24日星期五08:53:01 -0700

|行数:32

|消息ID:< 0c **************************** @ phx.gbl>

| MIME版本:1.0

|内容类型:text / plain;

| charset =" iso-8859-1"

|内容传输编码:7位

| X-Newsreader:适用于Windows 2000的Microsoft CDO

|线程索引:AcOaRudJlQHTzFPER3avOahP1Ww40A ==

| X-MimeOLE:由Microsoft MimeOLE制作V5.50.4910.0300

|新闻组:microsoft.public.dotnet.languages.csharp

|路径:cpmsftngxa06.phx.gbl

|外翻:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193841

| NNTP-Posting-Host:TK2MSFTNGXA12 10.40.1.164

| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp

|

|我有一个C#add in插入一个自定义的有效x控件

|到工作表。

|

|插入对象后,我可以找到对象

|通过Shapes和Worksheet.OLEObjects。什么我

|不能弄清楚如何使用对象

|以编程方式。

|

|我在Visual Studio中添加了对象的引用

|通过导入对象OCX进行项目。参考资料显示

|作为Interop.ActiveXLib和我的对象方法和

|

|下的对象浏览器中显示了属性interop.ActiveXLib.ImyControl和

| interop.ActiveXLib.myControlCtlClass。

|

|我已经尝试过各种铸造物品的安排

|从形状和工作表的OLEFormat.Object返回

|试图得到一些我可以直接使用的东西没有

|成功:

|

| ActiveXLib.myControlCtlClass mc =

| (ActiveXLib.myControlCtlClass)shape.OLEFormat.Object;

|

|我正在写Visual Studio .Net中的添加,它是

|在Excel 2002 Service Pack 1中运行。

|

|我想做的是能够使用:

| mc.myControlMethod(doIT)。

|

|感谢您的帮助

|

| Mark

|


你好Jeffrey,


我收到的错误信息是Specified Cast无效。

shape.OLEFormat.Object.GetType()。FullName;

的类型是System .__ ComObject。


我不知道如何取消引用System .__ ComObject来获得实际类型的

。 (如果我这样做,那么我可能知道如何获得

到我想要使用的对象)。

如果可能的话,请你发一个例子来说明这个怎么样? />
施放是为了使用实际物体吗?


谢谢


马克

-----原始消息-----
嗨Mark,

感谢您在此论坛中发帖。
什么是错误消息你转换
shape.OLEFormat.Object toActiveXLib.myControlCtlClass?
我想可能你可以使用typeof运算符来
确定类型ofshape.OLEFormat.Object。
也许你可以尝试使用ActiveXLib.ImyControl来获取
这个接口。你确定这个对象是
ActiveXLib.myControlCtlClass类型吗?
最好的问候,
Jeffrey Tan
Microsoft在线合作伙伴支持
获得安全! - www.microsoft.com/security
此帖子提供就像没有保证,
不授予任何权利。
--------------------
| Content-Class:urn:content-classes:message
|来自:马克 < mh*@yadayada.com>
|发件人:标记 < mh*@yadayada.com>
|主题:C#和OLEFormat.Object?
|日期:2003年10月24日星期五08:53:01 -0700
|行数:32
|消息ID:< 0c **************************** @ phx.gbl>
| MIME版本:1.0
|内容类型:text / plain;
| charset =" iso-8859-1"
| Content-Transfer-Encoding:7bit
| X-Newsreader:适用于Windows 2000的Microsoft CDO
|线程索引:AcOaRudJlQHTzFPER3avOahP1Ww40A ==
| X-MimeOLE:由Microsoft MimeOLE制作V5.50.4910.0300
|新闻组:microsoft.public.dotnet.languages.csharp
|路径:cpmsftngxa06.phx.gbl
|外翻:cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:193841 | NNTP-Posting-Host:TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp
|
|我有一个C#add in插入一个自定义的有效x
控件|进入工作表。
|
|插入对象后,我可以找到对象
|通过Shapes和Worksheet.OLEObjects。我是什么?不能弄清楚如何使用对象
|以编程方式。
|
|我在Visual Studio中添加了对象的引用
|通过导入对象OCX进行项目。参考
显示|作为Interop.ActiveXLib和我的对象方法和
|
|下的对象浏览器中显示了属性interop.ActiveXLib.ImyControl和
| interop.ActiveXLib.myControlCtlClass。
|
|我已经尝试了各种铸造物体的安排
|从形状和工作表的
OLEFormat.Object |返回试图获得我可以直接使用的东西没有
|成功:
|
| ActiveXLib.myControlCtlClass mc =
| (ActiveXLib.myControlCtlClass)
shape.OLEFormat.Object; |
|我正在写Visual Studio .Net中的添加,它是
|在Excel 2002 Service Pack 1中运行。
|
|我想做的是能够使用:
| mc.myControlMethod(doIT)。
|
|感谢您的帮助
|
| Mark
|



嗨Mark,


所以据我所知,您使用的演员阵容对您不起作用,因为当您在代码中使用强制转换时,.Net无法从Interop中检索类型信息

。 intermed com控件没有任何元数据,因此.Net不知道如何为您投射此对象。根据我的经验,我会建议你可以使用.Net反射方法在运行时动态获取类型信息然后调用你想要的方法。


假设有一个用户控件(ProgID是Project1.UserControl1),它有一个公共方法Test。测试方法只弹出一个窗口,其中包含

hello world。注册到系统后,您可以使用以下代码执行它。


[C#]

类型t = Type.GetTypeFromProgID(" Project1 .UserControl1");

Object o = Activator.CreateInstance(t);

t.InvokeMember(" Test",BindingFlags.InvokeMethod,null,o,null) ;


[VB.net]

Dim t As Type = Type.GetTypeFromProgID(" Project1.UserControl1")

Dim o As Object = Activator.CreateInstance(t)

o.Test()

在C#中,您必须使用InvokeMember来执行Test方法。但是在VB.net中,您可以使用o.Test语法来使用该方法。


请注意在VB.net中,如果您键入o。,则表示IntelliSense窗口不会告诉你对象有一个Test方法,因为在运行时从Project1.UserControl1控件中检索Test方法

信息。在编码时,没有足够的类型信息使IntelliSense能够找到测试方法。


此外,对于您的场景,控制对象可能有已经加载到excel工作表的相同进程,以便您可以修改

Activator.CreateInstance到Activator.GetObject。


请随时告诉我如果您有任何疑问。


这是否回答了您的问题?感谢您使用Microsoft NewsGroup!


Wei-Dong Xu

Microsoft产品支持服务

安全! - www.microsoft.com/security

此帖子原样是按原样提供的。没有保证,也没有授予任何权利。


I have a C# add in that inserts a custom active x control
into a worksheet.

Once the object is inserted i can locate the object
through both Shapes and Worksheet.OLEObjects. What I
can''t do is figure out how to use the object
programatically.

I added a reference to the object in the Visual Studio
project by importing the object OCX. The reference shows
up as Interop.ActiveXLib and my objects methods and
properties are shown in the object browser under both
interop.ActiveXLib.ImyControl and
interop.ActiveXLib.myControlCtlClass.

I have tried various arrangements of casting the object
returned from both Shapes and Worksheet''s OLEFormat.Object
trying to get something that I can use directly with no
success:

ActiveXLib.myControlCtlClass mc =
(ActiveXLib.myControlCtlClass) shape.OLEFormat.Object;

I am writting the add in Visual Studio .Net and it is
running in Excel 2002 Service Pack 1.

What I want to do is to be able to use:
mc.myControlMethod(doIT).

Thanks for the help

Mark

解决方案


Hi Mark,

Thanks for posting in this group.
What is the error message when you convert shape.OLEFormat.Object to
ActiveXLib.myControlCtlClass?
I think may be you can use the typeof operator to determine the type of
shape.OLEFormat.Object.
Maybe you can try to use ActiveXLib.ImyControl to get this interface.
Do you make sure that this object is a ActiveXLib.myControlCtlClass type?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Mark" <mh*@yadayada.com>
| Sender: "Mark" <mh*@yadayada.com>
| Subject: C# and OLEFormat.Object?
| Date: Fri, 24 Oct 2003 08:53:01 -0700
| Lines: 32
| Message-ID: <0c****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOaRudJlQHTzFPER3avOahP1Ww40A==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193841
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have a C# add in that inserts a custom active x control
| into a worksheet.
|
| Once the object is inserted i can locate the object
| through both Shapes and Worksheet.OLEObjects. What I
| can''t do is figure out how to use the object
| programatically.
|
| I added a reference to the object in the Visual Studio
| project by importing the object OCX. The reference shows
| up as Interop.ActiveXLib and my objects methods and
| properties are shown in the object browser under both
| interop.ActiveXLib.ImyControl and
| interop.ActiveXLib.myControlCtlClass.
|
| I have tried various arrangements of casting the object
| returned from both Shapes and Worksheet''s OLEFormat.Object
| trying to get something that I can use directly with no
| success:
|
| ActiveXLib.myControlCtlClass mc =
| (ActiveXLib.myControlCtlClass) shape.OLEFormat.Object;
|
| I am writting the add in Visual Studio .Net and it is
| running in Excel 2002 Service Pack 1.
|
| What I want to do is to be able to use:
| mc.myControlMethod(doIT).
|
| Thanks for the help
|
| Mark
|


Hi Jeffrey,

The error message i get is "Specified Cast is not valid".
The type of shape.OLEFormat.Object.GetType().FullName;
is System.__ComObject.

I don''t know how to dereference System.__ComObject to get
its actual type. (If I did then I might know how to get
to the object that I want to use).
If possible can you please post an example of how this
cast should made in order to work with the actual object?

Thanks

Mark

-----Original Message-----

Hi Mark,

Thanks for posting in this group.
What is the error message when you convert shape.OLEFormat.Object toActiveXLib.myControlCtlClass?
I think may be you can use the typeof operator to determine the type ofshape.OLEFormat.Object.
Maybe you can try to use ActiveXLib.ImyControl to get this interface.Do you make sure that this object is a ActiveXLib.myControlCtlClass type?
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| Content-Class: urn:content-classes:message
| From: "Mark" <mh*@yadayada.com>
| Sender: "Mark" <mh*@yadayada.com>
| Subject: C# and OLEFormat.Object?
| Date: Fri, 24 Oct 2003 08:53:01 -0700
| Lines: 32
| Message-ID: <0c****************************@phx.gbl>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcOaRudJlQHTzFPER3avOahP1Ww40A==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193841| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I have a C# add in that inserts a custom active x control| into a worksheet.
|
| Once the object is inserted i can locate the object
| through both Shapes and Worksheet.OLEObjects. What I
| can''t do is figure out how to use the object
| programatically.
|
| I added a reference to the object in the Visual Studio
| project by importing the object OCX. The reference shows| up as Interop.ActiveXLib and my objects methods and
| properties are shown in the object browser under both
| interop.ActiveXLib.ImyControl and
| interop.ActiveXLib.myControlCtlClass.
|
| I have tried various arrangements of casting the object
| returned from both Shapes and Worksheet''s OLEFormat.Object| trying to get something that I can use directly with no
| success:
|
| ActiveXLib.myControlCtlClass mc =
| (ActiveXLib.myControlCtlClass) shape.OLEFormat.Object;|
| I am writting the add in Visual Studio .Net and it is
| running in Excel 2002 Service Pack 1.
|
| What I want to do is to be able to use:
| mc.myControlMethod(doIT).
|
| Thanks for the help
|
| Mark
|

.



Hi Mark,

So far as I know, the cast you used will not work for you because .Net can''t retrieve the type information from Interop when you use cast in code
directly. There is no any metadata for the interoped com control so that .Net don''t know how to cast this object for you. From my experience, I''d
suggest you can use .Net reflection method to dynamically obtain the type information during runtime and then call the method you want.

Assume there is one user control (ProgID is "Project1.UserControl1") which has one public method Test. Test method only pop-up one window with
"hello world". After the registration to the system, you can use the codes below to execute it.

[C#]
Type t = Type.GetTypeFromProgID("Project1.UserControl1");
Object o = Activator.CreateInstance(t);
t.InvokeMember("Test", BindingFlags.InvokeMethod, null, o, null);

[VB.net]
Dim t As Type = Type.GetTypeFromProgID("Project1.UserControl1")
Dim o As Object = Activator.CreateInstance(t)
o.Test()

In C#, you have to use InvokeMember to execute the Test method. But in VB.net, you can use the o.Test syntax to use the method.

Please note in VB.net, if you type "o.", the IntelliSense window will not tell you there is one Test method for the object because the Test method
information is retrieved from Project1.UserControl1 control at runtime. At coding-time, there is no enough type information making IntelliSense to
find the Test Method.

Furthermore, for your scenario, the control object may have been loaded to the same process with excel worksheet so that you may modify the
Activator.CreateInstance to Activator.GetObject.

Please feel free to let me know if you have any questions.

Does this answer your question? Thank you for using Microsoft NewsGroup!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


这篇关于C#和OLEFormat.Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆