简体   繁体   中英

How to get name of user who has (Excel) file open on server, using Access VBA?

There are multiple Excel files on a server (or network shared storage location).

I have an Access document that needs access to these Excel files to execute a certain function.

When one of these files is open I can not execute my VBA function.

I check if someone is using the file. This is in the code below.

Is it is possible to also find out who is using a file. I would notify them to close the file(s).

Some of the things I tried (these are not all, but I can't find a few methods anymore that I tried too): https://chandoo.org/forum/threads/return-user-name-who-has-file-open.31447/ https://www.ozgrid.com/forum/forum/help-forums/excel-general/87346-vba-code-to-determine-who-has-file-open

In the last one they get the owner of the file and that is not the same as the one that is using the file at that moment. I tried it, but even then I sometimes get a username, but the username of the one that created the file and sometimes I get a SID (Security Identifier?).

Code to find out if the file is in use. This does not include anything to see who is using the file.

Sub TestFileOpened()
    Dim filesArray As Variant
    filesArray = Array("Map1.xlsx", "Map2.xlsx")
    Dim fileLocation As String
    fileLocation = "\\DESKTOP-NETWORK\SharedFolder\NetwerkTest\"
    Dim message As String
    
    For Each file In filesArray
        If IsFileOpen(fileLocation & file) Then
            message = message & vbNewLine & "File '" & file & "' is open!"
        'Else
        '    message = message & vbNewLine & "File '" & file & "' is closed!"
        End If
    Next file
    
    MsgBox message
    
End Sub

Function to check if the file is in use:

Function IsFileOpen(filename As String)
    Dim filenum As Integer, errnum As Integer
    
    On Error Resume Next
    filenum = FreeFile()
    
    Open filename For Input Lock Read As #filenum
    Close filenum
    errnum = Err
    On Error GoTo 0
    
    Select Case errnum
        Case 0
            IsFileOpen = False
        Case 70
            IsFileOpen = True
        Case Else
            Error errnum
    End Select
End Function

Access and Excel are able to do exactly that when you try to manually open the file. Superuser post: https://superuser.com/questions/845084/how-to-get-excel-to-show-username-of-person-that-has-file-open

Ok, i am not good in writing descent macro's, so modify code to suit your own needs!

This one should give the name of the user who has currently opened an Excel-sheet:

Sub InUse(filename As String)
    Dim f
    Dim i
    Dim x
    Dim inUseBy
    Dim tempfile
    tempfile = Environ("TEMP") + "\tempfile" + CStr(Int(Rnd * 1000))
    
    f = FreeFile
    i = InStrRev(filename, "\")
    If (i > 0) Then
        filename = Mid(filename, 1, i) + "~$" + Mid(filename, 1 + i)
    Else
        filename = "~$" + filename
    End If
    
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile filename, tempfile
    
    Open tempfile For Binary Access Read As #f
    Input #f, x
    Close (f)
    inUseBy = Mid(x, 2, Asc(x))
    fso.Deletefile tempfile
    Set fso = Nothing

    MsgBox "InUse by: " + inUseBy, vbOKOnly, "InUse"
    
End Sub

Example use:

InUse("T:\Book1.xlsx")

Things to note:

  1. This should be used when opening of a sheet fails (because of bein in-use)
  2. I did not find any documentation about this being the 'valid' way to do this.
  3. I do not know if this also works with shared excel sheets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM