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)

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

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.