Hi Wei-Dong Xu,
Thank you for your help. We are soooo close. Your
suggestions allow me to get a little farther.
I am confused by the Activator.GetObject call. The second
parameter is a URL. Since the control that I want to talk
to is in the same instance of Excel as the add in I don't
know what to use for this argument.
// Programatically add the object to the spreadsheet
Microsoft.Office.Interop.Excel.Shape shape =
ws.Shapes.AddOLEObject(
"MyDomain.MyCtl.1",
Type.Missing,
false,
false,
Type.Missing,
Type.Missing,
Type.Missing,
1, 1, 300, 300);
// Create argument to pass to control's method.
object [] args = {(object) "SomeArgument"};
// Get type of control
Type abCtl1Type = Type.GetTypeFromProgID(
"MyDomain.MyCtl.1");
// What do I use for the second argument if the instance
of abCtl1Type is the instance I automatically inserted
into the spreadsheet with the AddOLEObject call above?
ActiveXLib.MyCtlClass abCtl1Obj =
(ActiveXLib.MyCtlClass) Activator.GetObject
(abCtl1Type, ?????);
abCtl1Type.InvokeMember("OpenApplication",
BindingFlags.InvokeMethod, null, abCtl1Obj, args);
Thanks
Mark
[color=blue]
>-----Original Message-----
>Hi Mark,
>
>So far as I know, the cast you used will not work for you[/color]
because .Net can't retrieve the type information from
Interop when you use cast in code[color=blue]
>directly. There is no any metadata for the interoped com[/color]
control so that .Net don't know how to cast this object
for you. From my experience, I'd[color=blue]
>suggest you can use .Net reflection method to dynamically[/color]
obtain the type information during runtime and then call
the method you want.[color=blue]
>
>Assume there is one user control (ProgID[/color]
is "Project1.UserControl1") which has one public method
Test. Test method only pop-up one window with[color=blue]
>"hello world". After the registration to the system, you[/color]
can use the codes below to execute it.[color=blue]
>
>[C#]
>Type t = Type.GetTypeFromProgID("Project1.UserControl1");
>Object o = Activator.CreateInstance(t);
>t.InvokeMember("Test", BindingFlags.InvokeMethod, null,[/color]
o, null);[color=blue]
>
>[VB.net]
>Dim t As Type = Type.GetTypeFromProgID[/color]
("Project1.UserControl1")[color=blue]
>Dim o As Object = Activator.CreateInstance(t)
>o.Test()
>
>In C#, you have to use InvokeMember to execute the Test[/color]
method. But in VB.net, you can use the o.Test syntax to
use the method.[color=blue]
>
>Please note in VB.net, if you type "o.", the IntelliSense[/color]
window will not tell you there is one Test method for the
object because the Test method[color=blue]
>information is retrieved from Project1.UserControl1[/color]
control at runtime. At coding-time, there is no enough
type information making IntelliSense to[color=blue]
>find the Test Method.
>
>Furthermore, for your scenario, the control object may[/color]
have been loaded to the same process with excel worksheet
so that you may modify the[color=blue]
>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[/color]
Microsoft NewsGroup![color=blue]
>
>Wei-Dong Xu
>Microsoft Product Support Services
>Get Secure! -
www.microsoft.com/security
>This posting is provided "AS IS" with no warranties, and[/color]
confers no rights.[color=blue]
>
>
>.
>[/color]