Forum Discussion
Still got the same error after changing the file path to what you suggested. I also checked that Excel.exe is not running when I go to run the test.
This is mainly confusing to me as pretty much the same code in another file works fine.
var Excel = Sys.OleObject("Excel.Application");
Excel.Workbooks.Open("C:/Users/UserName/Documents/Test Cases.xlsx");
var xlsheet = Excel.ActiveWorkBook.WorkSheets("CategoryGroup");
I am also using this function because it was the easiest to understand when I started doing it.
You're example of where it is working someplace else has another significant difference... the name of the spreadsheet file is actually hardcoded in the code... the function you posted initially passes that filename in as a parameter. It's ENTIRELY possible that, when you're getting this error, that the filename may be incorrect... double check that by dropping a breakpoint in your function at excel.Workbooks.Open(filepath) and check the value of filepath.
You can also, step to the next line and then do a check to see if excel.ActiveWorkbook is actually a non-null value... again, just to double check... but I think the reason for your error is that ActiveWorkbook probably is null and so, therefore, Worksheets cannot be called.