c# - Excel VSTO workbook object returning empty -
excel vsto add-in puts button in ribbon.
i have pinpointed issue lies, don't know why giving me error or how fix it. more interesting, works fine if stay in same instance of excel. can run add-in multiple times no issues. open new instance of excel , comexception.
excel.workbook thisworkbook = globals.thisaddin.application.activeworkbook; //... //run code //run below function (my function) setthesecolumnheaders(thisworkbook.worksheets[1], somelist) //run more code globals.thisaddin.dispose(); //added line see if help, didn't change anything.
this doesn't give me issues first time run add-in, second time, thisworkbook
appears null object, calling worksheets[1]
on causes error.
not sure why happening - if open new instance of excel, , try running add-in, after running once, should create new objects, don't know why globals.thisaddin.application.activeworkbook
returning empty com object.
here steps involved issue:
- open new instance of excel
- click button in ribbon execute add-in
- custom form pops asking user parameters
- next custom form pops asking more input
- click ok on form (from [4]) , rest of program executes.
now, same sheet open ran vsto -
- open new instance of excel
- click button in ribbon execute add-in
- custom form pops asking user parameters
- next custom form pops asking more input
- click ok form closes - nothing happens.
now if close both instances , try running same parameters second time works fine.
i think know problem is. documentation says, _application.activeworkbook
can return null in cases. so, when open pop-up window, takes focus excel, , there no active workbook anymore. , maybe, if have 1 workbook, can guess option active. if have 2 workbooks, can return null. recommend store variable reference workbook before opening popups.
Comments
Post a Comment