How to check if Excel Workbook is locked
Parent page: Service Functions – Excel (QTP, VBScript)
Answers
Description
Connect to Excel through COM – Open workbook – Check property.
Public Function ReadOnlyWorkbook(ByVal sFileName) Dim objXLBook, objXLHandle Dim FSO, boolRC Set FSO = CreateObject("Scripting.FileSystemObject") boolRC = FSO.FileExists(sFileName) Set FSO = Nothing If Not boolRC Then ReadOnlyWorkbook = FALSE Exit Function End If ' Set objXLHandle = CreateObject("Excel.Application") objXLHandle.DisplayAlerts = False Set objXLBook = objXLHandle.WorkBooks.Open(sFileName) objXLBook.Activate 'Retrieve state ReadOnlyWorkbook = objXLHandle.ActiveWorkBook.ReadOnly 'release objects objXLBook.Close objXLHandle.Quit Set objXLBook = Nothing Set objXLHandle = Nothing End Function