Kartiki
Kartiki

Reputation: 95

Get active excel workbook object in javascript

Currently the code I am running is:

var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("filepath");
var excelsheet = excel_file.ActiveSheet;
var noofsubs = excelsheet.Cells(24,3).Value;

excel.workbboks.open commands opens an excel file. But how do I get reference to the already open excel file? maybe like

var excel_file = excel.ActiveWorkbooks.Activate("excelfilename");

Upvotes: 1

Views: 2911

Answers (1)

Tim Williams
Tim Williams

Reputation: 166316

Typically you'd use GetObject() if there's already an instance of Excel running and you want to get a reference to that. Note though that if there are >1 instance running, the one you get back can't be controlled from your script.

https://msdn.microsoft.com/library/7tf9xwsc%28v=vs.94%29.aspx

var XL;

try 
{ 
   // Existing instance running ? 
   XL = GetObject("", "Excel.Application"); 
} 
catch(e) 
{ 
   // Excel wasn't running: start new instance 
   XL = new ActiveXObject("Excel.Application"); 
} 

Upvotes: 1

Related Questions