Check the username who locks/uses a specific file

Hi folks,

Is there any solution to extract the name of the user who locks/has a specific file open?
With a basic try-catch, I can check if the file is editable or not, but I would need to notify the client who locks the file so they can act immediately.

Thank you!

Please refer below code, you can invoke it in Uipath.

Private Sub Workbook_Open()

Dim Folder As String
Dim FName As String
Dim strFilename As String
Folder = “C:”
FName = “data.xls”
If Not FileLocked(Folder & FName) Then
MsgBox “File " & Folder & FName & " is not open”
MsgBox "The file is locked by " & GetFileOwner(Folder, FName) & “.”
End If
End Sub

Function GetFileOwner(fileDir As String, fileName As String) As String

'On Error Resume Next
Dim secUtil As Object
Dim secDesc As Object
Set secUtil = CreateObject(“ADsSecurityUtility”)
Set secDesc = secUtil.GetSecurityDescriptor(fileDir & fileName, 1, 1)
GetFileOwner = secDesc.Owner
End Function

Function FileLocked(strFilename As String) As Boolean
On Error Resume Next
’ If the file is already opened by another process,
’ and the specified type of access is not allowed,
’ the Open operation fails and an error occurs.
Open strFilename For Binary Access Read Write Lock Read Write As #1
Close #1
’ If an error occurs, the document is currently open.
If Err.Number <> 0 Then
’ Display the error number and description.
'MsgBox “Error #” & Str(Err.Number) & " - " & Err.Description
FileLocked = True
End If
End Function