-2

I am using the COM to work with Excel 2007. When I use the following code, it opens to the first sheet by default.

Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx");

I need to select different sheets. I tryed using the following code from the DDTdriver code.

Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx", "sheet2", true);

But this does not work. I have tryed many other configs and still get nothing.

mnistic
  • 10,866
  • 2
  • 19
  • 33
dev
  • 191
  • 1
  • 11
  • I don't think [workbooks.open](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbooks-open-method-excel) has a way to specify a sheet, but after opening it, you can select the right sheet. – GolezTrol Jun 21 '18 at 10:52

1 Answers1

0

For Excel version 2013 or later :

According to learn.microsoft.com

this can be done with Sheets.Select method :

((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();

which in the code the selected sheet is 1

another method for older versions of Excel :

Excel.Worksheet sheet = (Excel.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);

where Sheet2 is the name of the sheet

Kaj
  • 806
  • 6
  • 16
  • Is there any way to do in Vb Script? – dev Jun 21 '18 at 11:16
  • Sorry but not familiar with VS Script, but this may help [WSH VBScript switch workSHEET in already open Excel workBOOK](https://stackoverflow.com/questions/17083715/wsh-vbscript-switch-worksheet-in-already-open-excel-workbook) – Kaj Jun 21 '18 at 11:18
  • Thanks For your Support. – dev Jun 21 '18 at 11:25
  • Above code is not working i am sorry, but is it a bug with Excel 2007? – dev Jun 21 '18 at 11:31
  • I've added another method to answer, try it please. – Kaj Jun 21 '18 at 11:33