How do I tell if an Excel 2007 spreadsheet is open and WHO has it open using VBScript?
I am trying to figure out whether or not an Excel workbook is currently open by another user and returning who that user is in my script.
I have already figured out who to determine if the workbook is currently open. It is a workaround, but I basically open the workbook and check if it is read-only. That works perfectly; I have tested it.
I know this is possible because Excel gives you the user who has the file open if you open it via the browser.
Here is my code (isWorkbookOpen.vbs):
Set objExcelTestWorkbook = CreateObject("Excel.Application")
objExcelTestWorkbook.DisplayAlerts = False 'doesn't display overwrite alert
testWorkbookFile = "I:\test_workbook.xlsx"
Set objBook = objExcelTestWorkbook.Workbooks.open(testWorkbookFile)
If objBook.ReadOnly Then
Wscript.echo "The file is read only"
Call EndScript
Else
Wscript.echo "The file is available"
Call EndScript
End If
Function EndScript
objExcelTestWorkbook.Workbooks.close
objExcelTestWorkbook.Quit
WScript.Echo "Closed " & testWorkbookFile
WScript.Quit
End Function
Also, I run this from the command line:
cscript isWorkbookOpen.vbs