代码之家  ›  专栏  ›  技术社区  ›  Anu

更新Excel文件,而不同时由多个用户打开该文件

  •  0
  • Anu  · 技术社区  · 7 年前

    脚本

    我有一个包含数据的Excel文件。有多个用户同时访问该文件。

    问题

    如果多个用户试图同时向该Excel文件输入数据,则会出现问题,因为一次只允许一个用户打开该文件。

    问题

    是否有任何方法可以更新Excel文件(例如:向单元格添加值、从单元格中删除值、查找特定单元格等),而无需打开它,以便多个用户可以使用Excel VBA同时更新它?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Anu    6 年前

    Update MSSQL from Excel

    TestFileOpened

    Sub fileCheck()
      Call TestFileOpened(plannerFilePathTemp)
        If fileInUse = True Then
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End Sub
    

    plannerFilePathTemp

    plannerFilePath = "C:\TEMP\XXX\xxx.xlsx"
    

    plannerFilePathTemp = "C:\TEMP\XXX\~$xxx.xlsx"
    

    ~$xxx.xlsx

    Call TestFileOpened(plannerFilePathTemp)

    Public fileInUse As Boolean
    Sub TestFileOpened(fileOpenedOrNot As String)
    Dim Folder As String
    Dim FName As String
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If objFSO.FileExists(fileOpenedOrNot) Then
        fileInUse = True
        MsgBox "Database is opened and using by " & GetFileOwner(fileOpenedOrNot) & ". Please wait a few second and click again", vbInformation, "Database in Use"
    Else
        fileInUse = False
    End If
    
    End Sub
    
    Function GetFileOwner(strFileName)
        Set objWMIService = GetObject("winmgmts:")
        Set objFileSecuritySettings = _
        objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")
        intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
    
        If intRetVal = 0 Then
           GetFileOwner = objSD.Owner.Name
        Else
           GetFileOwner = "Unknown"
        End If
    End Function
    

    Some tips to clear memory

    推荐文章