C# .NET Error Attaching to Running Instance of Office Application -


i working on excel addin using c#, visual studio 2012. trying instance of excel's application object keep track of active workbook , worksheet (activeworkbook, activeworksheet).

i see other related questions on have replies suggesting use following:

(excel.application)marshal.getactiveobject("excel.application"); 

i have tried using this:

(excel.application)globals.thisaddin.application; 

in both of cases nullreferenceexception. after looking @ workaround suggested here: http://support.microsoft.com/kb/316125/en-us, tried following test both methods.

public currentspreadsheet()     {         try         {             this.currentapplication = (excel.application)globals.thisaddin.application;          }         catch (nullreferenceexception)         {             messagebox.show("excel application object not registered. trying plan b..");              //getting excel's application object instance             int isection = 0, itries = 0;          tryagain:             try             {                 isection = 1; //attempting getactiveobject                 this.currentapplication = (excel.application)marshal.getactiveobject("excel.application");                 isection = 0; //getactiveobject succeeded resume or go normal error handling if needed                 this.currentapplication.visible = true;             }             catch (exception err)             {                 system.console.writeline("visual c# .net error attaching running instance of office application .. yet.");                 if (isection == 1)                 {                     //getobject may have failed because                     //shell function asynchronous; enough time has not elapsed                     //for getobject find running office application. wait                     //1/2 seconds , retry getobject. if try 20 times                     //and getobject still fails, assume other reason                     //for getobject failing , exit procedure.                     itries++;                     if (itries < 20)                     {                         system.threading.thread.sleep(500); // wait 1/2 seconds.                         goto tryagain; //resume code @ getobject line                     }                     else                     {                         messagebox.show("getobject still failing.  process ended.");                     }                  }                 else                 {                     //isection == 0 normal error handling                     messagebox.show(err.message);                 }             }         }       } 

the output is:

excel application object not registered. trying plan b.. getobject still failing.  process ended. 

in rare cases "plan b" work; don't see second message box. currentspreadsheet singleton , intend update during startup provided class thisaddin.

in thisaddin have like:

private currentspreadsheet css = currentspreadsheet.instance; private void thisaddin_startup(object sender, system.eventargs e)     {         ///...some code         css.updatecurrentspreadsheet(); } 

is there better way of getting application object? if not possible right during startup, there better way can keep track of active worksheet/workbook right startup of excel/my add-in? depending on application object (e.g. (excel.workbook)this.currentapplication.activeworkbook;) , event handlers keep track of current workbook , worksheet.

i tried using exceldna:

this.currentapplication = (excel.application)exceldna.integration.exceldnautil.application; 

this works of times gives error:

system.reflection.targetinvocationexception: exception has been thrown target of invocation. ---> exceldna.integration.xlcallexception: exception of type 'exceldna.integration.xlcallexception' thrown. 

keep in mind office add-in runs inside office process. never want find external process. boilerplate thisaddin class when use office project template hands application object looking on silver platter, use application property. first time can in startup event, make sure don't try earlier. don't drastic in constructor , don't initialize class members initialization expressions, wait until startup fires.

the relevant msdn page is here, "accessing object model of host application" section.


Comments