Is it possible to find out who has a workbook open when the workbook is not shared? i.e. User1 opens the workbook, User2 then tries to open the same workbook & gets the "do you want to be notified" message indicating someone else is using the workbook. I would then like to be able to run a macro to return the username of whoever has the workbook open so I could send an email but cannot find a way of accessing the "notification list".
Getting Username when workbook is opened Readonly
-
-
-
Re: Getting Username when workbook is opened Readonly
try this
Code
Display MoreSub TestVBA() '// Just change the file to test here Const strFileToOpen As String = "Cata.xls" If IsFileOpen(strFileToOpen) Then MsgBox strFileToOpen & " is already Open" & _ vbCrLf & "By " & LastUser(strFileToOpen), vbInformation, "File in Use" Else MsgBox strFileToOpen & " is not open", vbInformation End If End Sub Function IsFileOpen(strFullPathFileName As String) As Boolean Dim hdlFile As Long On Error GoTo FileIsOpen: hdlFile = FreeFile Open strFullPathFileName For Random Access Read Write Lock Read Write As hdlFile IsFileOpen = False Close hdlFile Exit Function FileIsOpen: '// Someone has it open! IsFileOpen = True Close hdlFile End Function Private Function LastUser(strPath As String) As String Dim strXl As String Dim strFlag1 As String, strflag2 As String Dim i As Integer, j As Integer Dim hdlFile As Long Dim lNameLen As Byte strFlag1 = Chr(0) & Chr(0) strflag2 = Chr(32) & Chr(32) hdlFile = FreeFile Open strPath For Binary As #hdlFile strXl = Space(LOF(hdlFile)) Get 1, , strXl Close #hdlFile j = InStr(1, strXl, strflag2) #If Not VBA6 Then '// Xl97 For i = j - 1 To 1 Step -1 If Mid(strXl, i, 1) = Chr(0) Then Exit For Next i = i + 1 #Else '// Xl2000+ i = InStrRev(strXl, strFlag1, j) + Len(strFlag1) #End If '// IFM lNameLen = Asc(Mid(strXl, i - 3, 1)) LastUser = Mid(strXl, i, lNameLen) End Function
-
Re: Getting Username when workbook is opened Readonly
As an alternative.
Code
Display MorePublic Function WhoHasFileOpen(strFileName As String) As String '// strFileName is a fully qualified path, filename & extension... Dim strTempFile As String Dim iPos As Integer Dim objFSO As Object iPos = InStrRev(strFileName, "\") strTempFile = Left(strFileName, iPos - 1) & "\~$" & Mid(strFileName, iPos + 1) Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strTempFile) Then WhoHasFileOpen = GetFileOwner(strTempFile) Else WhoHasFileOpen = vbNullString End If Set objFSO = Nothing End Function Private Function GetFileOwner(strFileName) As String Dim objWMIService As Object Dim objFileSecuritySettings As Object Dim objSD As Object Dim iRetVal As Integer Set objWMIService = GetObject("winmgmts:") Set objFileSecuritySettings = _ objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'") iRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD) If iRetVal = 0 Then GetFileOwner = objSD.Owner.Name Else GetFileOwner = "Unknown" End If Set objWMIService = Nothing Set objFileSecuritySettings = Nothing Set objSD = Nothing End Function
-
Re: Getting Username when workbook is opened Readonly
Worked perfectly, thank you.
-
Re: Getting Username when workbook is opened Readonly
You don't say which version worked, but second sight showed up a little bit of blindness on my part. The code in post #2 can retrieve the name of the file 'owner' directly rather than querying the Excel lock file, so the function WhoHasFileOpen can be shorted a little...
Code
Display MorePublic Function WhoHasFileOpen(strFileName As String) As String '// strFileName is a fully qualified path, filename & extension... Dim objFSO As Object Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strFileName) Then WhoHasFileOpen = GetFileOwner(strFileName) Else WhoHasFileOpen = vbNullString End If Set objFSO = Nothing End Function
-
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!