How to find the user who opened excel file file from vba

tdesilva

New Member
Joined
Aug 8, 2017
Messages
1
How to determine the user who is locking the excel file from vba.

I can find the owner of the excel file but owner is not always the last user

Private Sub clientList_Click()
Dim FileNm As String

Dim ret
FileNm = "C:\client list.xlsm"

ret = IsWorkBookOpen(FileNm)
If ret = True Then

'MsgBox "File is opened by " & GetFileOwner(FileNm) & "."
Else
MsgBox "File is Closed"
End If
End sub

Function IsWorkBookOpen(fileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open fileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Function GetFileOwner(fileName As String) As String
Dim secUtil As Object
Dim secDesc As Object
Dim File_Shortname As String
Dim fileDir As String
File_Shortname = Dir(fileName)
fileDir = Left(fileName, InStr(1, fileName, File_Shortname) - 1)
Set secUtil = CreateObject("ADsSecurityUtility")
Set secDesc = secUtil.GetSecurityDescriptor(fileDir & File_Shortname, 1, 1)
GetFileOwner = secDesc.owner
End Function

Thanks, if you can let me how to find the user who is locking the excel file
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top