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:

  1. open new instance of excel
  2. click button in ribbon execute add-in
  3. custom form pops asking user parameters
  4. next custom form pops asking more input
  5. click ok on form (from [4]) , rest of program executes.

now, same sheet open ran vsto -

  1. open new instance of excel
  2. click button in ribbon execute add-in
  3. custom form pops asking user parameters
  4. next custom form pops asking more input
  5. 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

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -