简体   繁体   中英

Check if a shared workbook is already open by another user

I am developing a program where some data is saved in an Excel file. It's a shared file, so everyone can use it. Before I open the file, I want to know if the file is already open for write by another user. At the moment I'm using the code below:

    Dim app As New Excel.Application
    Dim book As Excel.Workbook
    Dim sheet As Excel.Worksheet

    If app.Workbooks.CanCheckOut("O:\T_Fiabilidade_QMM6\Recursos\Informáticos\SW Occupation rate\RPRS.xlsx") = True Then
        MsgBox("Available")
    Else
        MsgBox("Não disponível")
    End If
End Sub

But it always returns me the message "Não disponível" even if the file is closed. Can anyone can help me?

Try to check ReadOnly property of the workbook,
if its true so somebody is using the workbook:

Dim app As New Application
Dim book As Workbook
Dim sheet As Worksheet

Public Function IsWokBookInUse() As Boolean
    book = app.Workbooks.Open("")

    If book.ReadOnly = True Then
        MsgBox("the file is in use")
        ' optional: close workbook...'
        book.Close()
        app.Quit()
        Return True
    Else
        MsgBox("o.k")
        Return False
    End If
End Function

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