Suppose you have a XLSM that needs to open a XLSX and have exclusive access to it. However if you are working on a network, there is the possibility that another user already has this XLSX open.
I already have code that will detect if another user has the XLSX open or not. The problem I have is finding a way to determine the name of the user that is currently using this workbook.
What I have tried so far:
1. WriteReservedBy = No point discussing this one further. It doesn't work (websites say so + I tested myself).
2. I found a custom function that looked extremely promising (see code at end of this post) but when tested it couldn't return the name of the user (the strXL returned a complete mess. There was no readable name in it).
My current idea for solving (but I would like help on):
I notice that when another user has a workbook open on a network, Windows creates a hidden workbook while it is open. This hidden workbook exists in the same directory and has the same file name but with a prefix of "~$" added. Now the interesting thing is this: If you look at file properties (in Windows Explorer) the Owner property for the open Workbook remains that of the creator. However the Owner property for the hidden workbook is that of the user who currently has the workbook open. So how do I get the code to return this?
APPENDIX:
This is the code that didn't return the user name. However the LastUser function could be replaced with my idea above.
Option Explicit
'// Just change the file to test here
Private Const strFileToOpen As String = "C:\Test.xlsx"
Public Sub TestVBA()
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
Private 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
'// http://www.xcelfiles.com
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
Private Function LastUser(strPath As String) As String
'// Code by Helen from http://www.visualbasicforum.com/index.php?s=
'// This routine gets the Username of the File In Use
'// Credit goes to Helen for code & Mark for the idea
'// Insomniac for xl97 inStrRev
'// Amendment 25th June 2004 by IFM
'// : Name changes will show old setting
'// : you need to get the Len of the Name stored just before
'// : the double Padded Nullstrings
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
Bookmarks