Get username of open file (.xlsx & .pdf)

crookesa

Board Regular
Joined
Apr 11, 2013
Messages
88
I am using the following code I found on Ryan Wells website which is allows me to get the username of the person who has a file open.
It works great for my purpose, only extension I was hoping for would it be possible to find their actual name rather than username from a directory perhaps?

VBA Code:
Function Excel_File_in_use_by(FilePath As String) As String
Dim strTempFile As String
Dim iPos As Integer, iRetVal As Integer
Dim objFSO As Object, objWMIService As Object, objFileSecuritySettings As Object, objSD As Object
iPos = InStrRev(FilePath, "\")
strTempFile = left(FilePath, iPos - 1) & "\~$" & Mid(FilePath, iPos + 1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strTempFile) Then
    Set objWMIService = GetObject("winmgmts:")
    Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strTempFile & "'")
    iRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
    If iRetVal = 0 Then
        Excel_File_in_use_by = objSD.Owner.Name
    Else
        Excel_File_in_use_by = "unknown"
    End If
    Set objWMIService = Nothing
    Set objFileSecuritySettings = Nothing
    Set objSD = Nothing
Else
    Excel_File_in_use_by = vbNullString
End If
Set objFSO = Nothing
End Function

I am using Application.UserName for another piece of code but that takes it from the Application and open to people changing their user names in Excel.

Is it possible to get the above code also to work for PDF files? It doesn't seem to create a temporary ~$filename. So not sure what happens, I do get the 1004 error and have use the code below to check if it's open.

Code:
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 53:   IsWorkBookOpen = False
    Case 70:   IsWorkBookOpen = True
    Case Else: Error ErrNo
    End Select
End Function
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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