How to check if Excel Workbook is locked

Posted by Albert Gareev on Apr 07, 2010 | Categories: How toMS Excel Data

Parent page: Service Functions – Excel (QTP, VBScript)



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)

'Retrieve state
 ReadOnlyWorkbook = objXLHandle.ActiveWorkBook.ReadOnly

'release objects


 Set objXLBook = Nothing
 Set objXLHandle = Nothing
End Function

  • Leave a Reply

    * Required
    ** Your Email is never shared

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.