VBA脚本来检查文本文件是否打开

我正在检查一个文件是否打开,这是一个.txt文件

Private Sub CommandButton1_Click() Dim strFileName As String ' Full path and name of file. strFileName = "D:\te.txt" ' Call function to test file lock. If Not FileLocked(strFileName) Then ' If the function returns False, open the document. MsgBox "not open" Else MsgBox "open" End If End Sub Function FileLocked(strFileName As String) As Boolean On Error Resume Next ' If the file is already opened by another process, ' and the specified type of access is not allowed, ' the Open operation fails and an error occurs. Open strFileName For Binary Access Read Write Lock Read Write As #1 Close #1 ' If an error occurs, the document is currently open. If Err.Number <> 0 Then ' Display the error number and description. MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description FileLocked = True Err.Clear End If End Function 

事实certificate,使用记事本打开时,.txt不会locking文件,因此无法知道.txt文件是否打开。 因此,如果这个.txt文件是在写字板或樱花等打开,你的代码应该工作或至less其他networking代码应该工作。