MulgraveTester
13 years agoFrequent Contributor
Determine if Excel workbook is already open
How do I determine if an Excel workbook is already open? Any help would be appreciated - thanks.
I have tried the two methods below.
Test1 fails if the workbook is not open because it can't find the object.
Test2 fails if the workbook is open because workbooks are not parameterised.
'VBScript
sub test1 'Reference the workbook directly
set Excel = sys.OleObject("Excel.Application")
delay 3000
excel.Visible = true
if not (Excel.workbooks("myworkbook.xls") is nothing) then
log.message("Workbook is already open")
end if
end sub
sub test2 'Search for the workbook in the collection of open workbooks
set Excel = sys.OleObject("Excel.Application")
delay 3000
excel.Visible = true
for i = 0 to Excel.Workbooks.count -1
if Excel.workbooks(i).name = "myworkbook.xls" then
log.message("Workbook is already open")
end if
next
end sub
I have tried the two methods below.
Test1 fails if the workbook is not open because it can't find the object.
Test2 fails if the workbook is open because workbooks are not parameterised.
'VBScript
sub test1 'Reference the workbook directly
set Excel = sys.OleObject("Excel.Application")
delay 3000
excel.Visible = true
if not (Excel.workbooks("myworkbook.xls") is nothing) then
log.message("Workbook is already open")
end if
end sub
sub test2 'Search for the workbook in the collection of open workbooks
set Excel = sys.OleObject("Excel.Application")
delay 3000
excel.Visible = true
for i = 0 to Excel.Workbooks.count -1
if Excel.workbooks(i).name = "myworkbook.xls" then
log.message("Workbook is already open")
end if
next
end sub