简体   繁体   English

如何使用Access VBA获取在服务器上打开(Excel)文件的用户名?

[英]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).服务器(或网络共享存储位置)上有多个 Excel 文件。

I have an Access document that needs access to these Excel files to execute a certain function.我有一个 Access 文档,需要访问这些 Excel 文件才能执行某个功能。

When one of these files is open I can not execute my VBA function.当这些文件之一打开时,我无法执行我的 VBA 函数。

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我尝试过的一些东西(这些不是全部,但我再也找不到一些我尝试过的方法): 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?).我试过了,但即便如此,我有时也会得到一个用户名,但有时会得到一个创建文件的用户名,有时我会得到一个 SID(安全标识符?)。

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.当您尝试手动打开文件时,Access 和 Excel 能够做到这一点。 Superuser post: https://superuser.com/questions/845084/how-to-get-excel-to-show-username-of-person-that-has-file-open超级用户帖子: 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:这个应该给出当前打开 Excel 工作表的用户的姓名:

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我不知道这是否也适用于共享的 Excel 表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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