c# - How to handle events from embedded Excel.OleObjects or Excel.Shapes -


i'm working on c# , vb.net ports of old vba program. has lots of msforms/oleobjects embedded in commandbutton or images.

my first thought declare buttons microsoft.vbe.interop.forms.commandbuttonbut leads com exception system._com type can't cast ...forms.commandbutton. if try more generic version of this solution, don't find items, , if try go through vbcomponets note sheets in workbook, none of controls:

foreach (vbcomponent xxx in globals.thisworkbook.vbproject.vbcomponents) {     interaction.msgbox(xxx.name);     interaction.msgbox(xxx.tostring); } 

thus of these controls not in .vbcomponets, can find them oleobjects in thisworkbook.worksheets(n).oleobjects (this counterintutive me, don't understand system begin with).

how handle click action such object?

i'm assuming need using excel.oleobjectevents_event interface, can't seem figure out how. if try make custom events delegates, don't seem able assign them oleobjects. if use actionclickeventhandler.createdelegate can huge variety of errors makes me think that's dead end.

the official documentation ms doesn't seem helpful, though did introduce me idea of verb, i'm looking into. far has produced com errors along lines of "application failed start."

even trying use 1 of 2 standard events, .gotfocus, pull 0x80040200 error. example:

excel.oleobject buttoncatcher = globals.thisworkbook.worksheets(1).oleobjects("commandbutton1"); buttoncatcher.gotfocus += commandbutton1_click; 

throws comexception exception hresult: 0x80040200 @ second line. button enabled, checked after looking code number office dev site.

trying more generic approach within code sheet containing controls:

object commandbuttonstart = this.gettype().invokemember("commandbutton1", system.reflection.bindingflags.getproperty, null, this, null); 

throws missing method error.

any appreciated, seems should obvious , i'm missing it.


**edit: have found can cast these controls excel.shape doesn't me closer running function or sub vsto. i'm playing excel.shape.onaction requires vba sub called. presumably call vba sub calls sub vsto long vsto com visible. seems round-about , i'd last resort.

have tried using newlatebinding.lateget?

using msforms = microsoft.vbe.interop.forms; using microsoft.visualbasic.compilerservices;  ...  msforms.commandbutton commandbutton1 = (msforms.commandbutton)newlatebinding.lateget(globals.thisworkbook.worksheets(1), null, "commandbutton1", new object[0], null, null, null);                 commandbutton1.click += new microsoft.vbe.interop.forms.commandbuttonevents_clickeventhandler(commandbutton1_click); 

it's referenced on msdn in vsto forums , in old blog post.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -