show whos got an Excel file open (1 Viewer)

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
I have a form that on open checks whether an Excel file on the server is open or not, be fore certain other functions occur. If the file is open I get a message, if the file is not open then the queries run. What I would like is to know who has the Excel file open. Can this be written into the code below,
Code:
Private Sub Form_Open(Cancel As Integer)
DoCmd.Maximize
DoCmd.ShowToolbar "Ribbon", acToolbarNo
Dim location As String
Dim wbk As Workbook
location = "F:\Victoria Line\Bombardier\Maintenance\PLANNING DANNY AND LAWRENCE\KM run\Km  Forecast\Burst Report & Predict - MASTER.xlsx"
Set wbk = Workbooks.Open(location)
'Check to see if file is already open
If wbk.ReadOnly Then
  ActiveWorkbook.Close
    MsgBox “Burst Report file is open and mileage cannot be updated"
    GoTo CONTINUE:
End If
ActiveWorkbook.Close
DoCmd.SetWarnings False
On Error Resume Next
DoCmd.OpenQuery "kmnow_delete_qry"
On Error Resume Next
DoCmd.OpenQuery "kmnow_insert_qry"
On Error Resume Next
DoCmd.SetWarnings True

CONTINUE:
DoCmd.OpenForm FormName:="cal equip 2 form"
DoCmd.OpenForm FormName:="training_overdue"
End Sub

Any help very welcome, as this is now well beyond my understanding of VBA.
Many thanks
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:30
Joined
Apr 27, 2015
Messages
6,337
This is the second post I have seen you initiate regarding an excel file. Why not convert it to a table and solve all your headaches in one fell swoop?
 

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
NauticalGent, thanks for your comments. I feel I may not have explained my situation clearly enough.
I get the mileage, km, from the excel file Burst Report and using an Append query it is added to a table, as you suggest. The problem is, if the Burst Report is already open by another user on the network, I cannot get the current mileage.
It would be very useful to know who has the file open, so I could request they close it as soon as they are finished.
I hoped it might be possible by modify the code I included in my original post.
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:30
Joined
Apr 27, 2015
Messages
6,337
Precisely. My recommendation is to convert that Excel file (Burst Report) to an Access Table with an applicable form and call it done.
 

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
Unfortunately I cannot do anything with the source excel file, Burst Report and have to work with what there is.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:30
Joined
May 7, 2009
Messages
19,238
you may try this one, copy it in Standard Module
in VBE:

Code:
Public Function WhoHasLockMyFile(serverName As Variant, fileFullPath As String) As String

    Dim varServer, varFile, objFS, varFoundNone, objRes
    Dim FindPos As Long
    ' first, get the server name we want to work with
    varServer = serverName
    varFile = fileFullPath
    'varServer = InputBox("Server name to check")
    
    ' get the local path of the file to check
    
    'varFile = InputBox _
    '        ("Full path and filename of the file on the " & _
    '            "server (use the local path as if you were " & _
    '            "at the server console)")
    
    ' bind to the server’s file service
    'Set objFS = GetObject("WinNT://" & varServer & _
    '"/lanmanserver,fileservice")
    'Set objFS = GetObject("WinNT://" & varServer & _
    '"/lanmanserver.fileservice")
    Set objFS = GetObject("WinNT://" & varServer & _
    "/LanmanServer")
    
    ' scan through the open resources until we
    ' locate the file we want
    varFoundNone = True
    
    ' use a FOR...EACH loop to walk through the
    ' open resources
    For Each objRes In objFS.Resources
        ' does this resource match the
        ' one we’re looking for?
         If (Not objRes.User = "") And (Not right(objRes.User, 1) = "$") Then
        'If objRes.path = varFile Then
            ' we found the file - show who’s got it
                FindPos = InStr(1, objRes.path, varFile, 1)
                If FindPos <> 0 Then
            
                    varFoundNone = False
                    WhoHasLockMyFile = objRes.User
                    'MsgBox objRes.path & _
                    '    " is opened by " & objRes.User
                    Exit For
                End If
            End If
    Next
    ' if we didn’t find the file open, display a msg
    'If varFoundNone = True Then
    '        MsgBox "Didn’t find that file opened " & _
    '                "by anyone."
    'End If
    Set objRes = Nothing
    Set objFS = Nothing
End Function


to call it on your existing code:

Code:
...
...
Dim strUser As String
If wbk.ReadOnly Then
	wbk.Close
	strUser = WhoHasLockMyFile("computerName","F:\Bombardier\Maintenance\Planning Danny And Lawrence\Km Forecast\Burst Report & Prediction - MASTER.xlsx")
	MsgBox "Burst file is opened " & iif(struser<>"", "by: " & strUser & ",", "") & " mileage cannot be updated"
	GOTO CONTINUE"
End If

...
...


'honestly haven't tested the function, so you'll
got a chance to test it.

"ComouterName" is the name of the compuetr (server) where the dule is located.
 
Last edited:

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
Thanks arnelgp, problem is I haven't made it clear I'm doing this in Access, so your code stops part way through,

Code:
Public Function WhoHasLockMyFile(serverName As Variant, fileFullPath As String) As String
Dim varServer, varFile, objFS, varFoundNone, objRes
Dim FindPos As Long
' first, get the server name we want to work with
varServer = "F:\Km Forecast\"
varFile = "Burst Report & Predict - MASTER.xlsx"
'varServer = InputBox("Server name to check")

' get the local path of the file to check

'varFile = InputBox _
' ("Full path and filename of the file on the " & _
' "server (use the local path as if you were " & _
' "at the server console)")

' bind to the server’s file service
'Set objFS = GetObject("WinNT://" & varServer & _
'"/lanmanserver,fileservice")
'Set objFS = GetObject("WinNT://" & varServer & _
'"/lanmanserver.fileservice")
Set objFS = GetObject("WinNT://" & varServer & _
"/LanmanServer")

' scan through the open resources until we
' locate the file we want
varFoundNone = True

' use a FOR...EACH loop to walk through the
' open resources
For Each objRes In objFS.Resources
' does this resource match the
' one we’re looking for?
If (Not objRes.User = "") And (Not Right(objRes.User, 1) = "$") Then
'If objRes.path = varFile Then
' we found the file - show who’s got it
FindPos = InStr(1, objRes.Path, varFile, 1)
If FindPos <> 0 Then

varFoundNone = False
WhoHasLockMyFile = objRes.User
'MsgBox objRes.path & _
' " is opened by " & objRes.User
Exit For
End If
End If
Next
' if we didn’t find the file open, display a msg
'If varFoundNone = True Then
' MsgBox "Didn’t find that file opened " & _
' "by anyone."
'End If
Set objRes = Nothing
Set objFS = Nothing
End Function

Is it possible to give a version of this that will work with MS Access. I have done exactly as you suggested, so hopefully there's a simple tweek to your code,
Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:30
Joined
May 7, 2009
Messages
19,238
this code is MS Access, i already
give you the code on how to call it.
you don't need to insert anything on the
function.

remove the code you added and just call it

strWhoHasLock = WhoHasLockMyFile("servername","F:\KM Forecast\Burst Report & Predict - MASTER.xlsx")

!!!
note the "servername", this is not the folder name.
this is the Name of the Computer (sharing computer) where your
file is located.
 

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
we don't have access to the server name, it could be opened on several machines by a minimum of 5 users. we can only see the path as F:\....etc


we tried running the code but it keeps stumbling at

Code:
    For Each objRes In objFS.Resources

when you try to open excel manually, it says to us user "johnc1" has the file open and gives an option of using read only, etc. we just want that username so we know who to ask to log out.
 

NauticalGent

Ignore List Poster Boy
Local time
Yesterday, 22:30
Joined
Apr 27, 2015
Messages
6,337
Arnelgp, your coding skills are truly marvelous to behold. I will be adding this bit to my ever growing library, just in case!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:30
Joined
May 7, 2009
Messages
19,238
I have found a hack in the ner but requires to chanfe your excel file to macro enabled (. xlsm)
 

109bow

Registered User.
Local time
Today, 03:30
Joined
Oct 24, 2007
Messages
134
Unfortunately arnelgp, there is no chance of being able to modify the excel file to .xlsm, as I say I'm stuck with what I've got.
Thanks
 

Solo712

Registered User.
Local time
Yesterday, 22:30
Joined
Oct 19, 2012
Messages
828
I have found a hack in the ner but requires to chanfe your excel file to macro enabled (. xlsm)

That's correct. If you go peer-to-peer with no server file service you would have
to restrict access to the Excel so it can be open only from within MS Access. You can do that by placing a password on the Excel file (that noone knows but the admin) and lifting it when you open the file (through Access). Follow the thread here:

https://bytes.com/topic/access/answ...rotected-excel-file-access-disable-protection

Best,
Jiri
 

Users who are viewing this thread

Top Bottom