我正在开发一个程序,其中一些数据保存在Excel文件中。这是一个共享文件,因此每个人都可以使用它。在打开文件之前,我想知道文件是否已经打开供其他用户写入。目前,我正在使用以下代码:
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
但是即使关闭了文件,它始终会向我返回消息“Nãodisponível”。谁能帮助我?
答案 0 :(得分:1)
尝试检查工作簿的ReadOnly
属性,
如果是真的,那么有人正在使用工作簿:
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