Home » excel » office js – Can't get active worksheet or selected range when other Excel instance has focus

office js – Can't get active worksheet or selected range when other Excel instance has focus

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am maintaining an Office Add-In that receives data from a server and writes it to the current selection in the active worksheet.

When I open a seconds workbook, the Add-In in the first workbook is not able to get the active worksheet anymore.

When I focus the first workbook it’s working again.

I am trying to get the active worksheet like this:

Excel.run(function (ctx) {
  var sheet = ctx.workbook.worksheets.getActiveWorksheet();
  // insert code following ...
}

I get the following error:

Error at WorksheetCollection.getActiveWorksheet
InvalidSelection: The current selection is invalid for this operation.
 at Anonymous function 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:12439:6)
 at lib$es6$promise$$internal$$tryCatch 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9240:8)
 at lib$es6$promise$$internal$$invokeCallback 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9250:8)
 at lib$es6$promise$$internal$$publish 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9226:9)
 at lib$es6$promise$asap$$flush 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9060:8)

Getting the selected range of the workbook doesn’t work either:

Excel.run(function (ctx) {
  var range = ctx.workbook.getSelectedRange().getCell(0, 0);
  // ...
}

Similar Error:

Error at Workbook.getSelectedRange
InvalidSelection: The current selection is invalid for this operation.
 at Anonymous function 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:12439:6)
 at lib$es6$promise$$internal$$tryCatch 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9240:8)
 at lib$es6$promise$$internal$$invokeCallback 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32.16.01.debug.js:9250:8)
 at lib$es6$promise$$internal$$publish 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9226:9)
 at lib$es6$promise$asap$$flush 
  (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.debug.js:9060:8)

This problem has just appeared recently.

Update: It turned out that the previous statement was wrong, behavior was always like this (tested with a new MSI installation without any updates installed).

I was not able to reproduce it by starting two instances from Visual Studio.

In a Word Add-In ctx.document.getSelection() works as expected.

Is there anything I can do about this?

How to&Answers:

My impression is that this has been working this way. When you say that it used to work before, could you provide more information on which build/version of Excel it used to work? We will do more investigation to see if this is a behavior change.

If the workbook is not active, then the getActiveWorksheet() is expected to fail as it’s not in active state.

UPDATE:
Now we support Workbook.getActiveWorksheet() to return the active worksheet on the workbook no matter the workbook is active or not. If you want to keep track of the selected range, you may use Workbook.onSelectionChanged event.

Answer:

If the second workbook is selected the initial workbook – and therefore your desired worksheet – will no longer be tagged as the activeworksheet.

You should be careful using activeworksheet when switching focus in excel workbooks.

My advice would be to catch and store the name of the activeworksheet in a variable, or to just plain hard code it, then use this variable in all future references

Answer:

A workaround is to register a WorksheetActivatedEvent, track the worksheet name in the event, and then you could use get the selected worksheet from name, when your workbook lost the focus.

it would be great if you could share your scenario, which help us understand more about the issue.