Juriemagic
New Member
Hi good people!,
I have searched and found this code:
This code works well, however it fails to supply the current user name of the person who has the file open. It says
I have posted this on Mrexcel.com last week but failed to receive response that really helped me, here:http://www.mrexcel.com/forum/excel-...asic-applications-tell-if-file-read-only.html
I am asking for help with this please, all and any help will be accepted with great humility and respect..Thank you..
I have searched and found this code:
Code:
Const strFileToOpen As String = "G:\All Users\Jurie\Test Book 2.xlsx"
If IsFileOpen(strFileToOpen) Then
MsgBox strFileToOpen & " is already Open" & _
vbCrLf & "By " & LastUser(strFileToOpen), vbInformation, "File in Use"
Else
Workbooks.Open "G:\All Users\Jurie\Test Book 2.xlsx"
End If
End Sub
Function IsFileOpen(strFullPathFileName As String) As Boolean
'// VBA version to check if File is Open
'// We can use this for ANY FILE not just Excel!
'// Ivan F Moala
'// [url]http://www.xcelfiles.com[/url]
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 Lock Read Write As hdlFile
IsFileOpen = False
Close hdlFile
Exit Function
FileIsOpen:
'// Someone has it open!
IsFileOpen = True
Close hdlFile
End Function
Function LastUser(path 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 path For Binary As #1
text = Space(LOF(1))
Get 1, , text
Close #1
Dim n As Long, nam As String
For n = j - 1 To 1 Step -1
If Mid(text, n, 1) = Chr(0) Then Exit For
nam = Mid(text, n, 1) & nam
Next
LastUser = Name
End Function
This code works well, however it fails to supply the current user name of the person who has the file open. It says
"file name" is already open by
I have posted this on Mrexcel.com last week but failed to receive response that really helped me, here:http://www.mrexcel.com/forum/excel-...asic-applications-tell-if-file-read-only.html
I am asking for help with this please, all and any help will be accepted with great humility and respect..Thank you..