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?
I am using
Is it possible to get the above code also to work for PDF files? It doesn't seem to create a temporary
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