The_Kurgan
Active Member
- Joined
- Jan 10, 2006
- Messages
- 270
I have code that tells whether a particular Excel workbook is open, but would like to also incorporate the name of the user who has it open. It should mimic the default Excel message, but I can't use that default message for various reasons. Has anyone used or written such code? I've found nothing in MrExcel and the only other resource I've found is here: Who has File Open (VBA Excel 2003, Win XP) | Windows Secrets Lounge.
I tried their bit of code:
but it pukes on the InStrRev (Invalid procedure call or argument). I think that even if I could get the InStrRev to work, it would produce gibberish, as that's what the "text" variable produces.
I'd be open to any other ideas. Thanks in advance!
I tried their bit of code:
Code:
Public Sub Get_a_Name()
'// Just change the file to test here
Const strFileToOpen As String = "X:\Mgmt\Aged Warehouse Reports\Aging Workbooks (Do Not Open)\Aged 30-45.xlsm"
If IsFileOpen(strFileToOpen) Then
MsgBox strFileToOpen & " is already Open" & _
vbCrLf & "By " & LastUser(strFileToOpen), vbInformation, "File in Use"
MsgBox ActiveWorkbook.WriteReservedBy
Else
MsgBox strFileToOpen & " is not open"
MsgBox ActiveWorkbook.WriteReservedBy
End If
End Sub
Function IsFileOpen(strFullPathFileName As String) As Boolean
Dim hdlFile As Long
'// Error is generated if you try
'// opening a File for ReadWrite lock >> MUST BE OPEN!
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
Function LastUser(strPath As String) As String
Dim text As String
Dim strFlag1 As String, strflag2 As String
Dim i As Integer, j As Integer
strFlag1 = Chr(0) & Chr(0)
strflag2 = Chr(32) & Chr(32)
Open strPath For Binary As #1
text = Space(LOF(1))
Get 1, , text
Close #1
j = InStr(1, text, strflag2)
i = InStrRev(text, strFlag1, j) + Len(strFlag1) 'PUKES HERE (Invalid procedure call or argument)
LastUser = Mid(text, i, j - i)
End Function
but it pukes on the InStrRev (Invalid procedure call or argument). I think that even if I could get the InStrRev to work, it would produce gibberish, as that's what the "text" variable produces.
I'd be open to any other ideas. Thanks in advance!