This code add a worksheet to my workbook in a VSTO excel workbook application via a ribbon:
Globals.ThisWorkbook.Worksheets.Add();
Excel.Window window = e.Control.Context;
Excel.Worksheet activeWorksheet =((Excel.Worksheet)window.Application.ActiveSheet);
The added worksheet is also the active one (obviously). How can I get existing worksheets in workbook application - ideally by name?
This gives me (before I add the worksheet above) 2 correctly as there are 2 worksheets:
var nows = Globals.ThisWorkbook.Worksheets.Count;
One would think that I can access the worksheet at least by index like so:
var ws = Globals.ThisWorkbook.Worksheets[0];
but this throws this exception:
$exception {"Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))"} System.Runtime.InteropServices.COMException
Any ideas? Ideally, I would like to access the worksheets via name. Thanks!