用于检查文本文件是否打开的VBA脚本

用于检查文本文件是否打开的VBA脚本,vba,excel,text,Vba,Excel,Text,我正在检查一个文件是否打开,这是一个.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 r

我正在检查一个文件是否打开,这是一个.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
Private子命令按钮1\u单击()
将strFileName设置为字符串
'文件的完整路径和名称。
strFileName=“D:\te.txt”
'调用函数来测试文件锁。
如果未锁定文件(strFileName),则
'如果函数返回False,请打开文档。
MsgBox“未打开”
其他的
MsgBox“打开”
如果结束
端接头
函数FileLocked(strFileName为字符串)为布尔值
出错时继续下一步
'如果文件已由另一个进程打开,
'并且不允许指定的访问类型,
'打开操作失败,出现错误。
打开二进制访问读写锁定读写为#1的strFileName
关闭#1
'如果发生错误,则文档当前处于打开状态。
如果错误号为0,则
'显示错误号和说明。
MsgBox“Error#”和Str(Err.Number)&“-”和Err.Description
FileLocked=True
呃,明白了
如果结束
端函数

使用记事本打开时,会发现.txt不会锁定文件,因此无法知道.txt文件是否打开。因此,如果该.txt文件是用写字板或Sakura等打开的,那么您的代码应该可以工作,或者至少来自网络的其他代码应该可以工作

我发现,如果使用FileSystemObject打开文本文件,则该文件不会被锁定,其他用户仍可对其进行编辑。作为一种潜在的解决方法,您可以创建一个带有单个位的文件,以指示另一个文件何时正在使用,并在代码中包括检查该位。以下是我的代码作为一个粗略的示例:

'FSO parameters
Const ForAppending = 8
Const ForReading = 1
Const ForWriting = 2

Sub WriteToFile()
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Check the current lock bit (1 is locked, 0 is unlocked)
    Set FileLock = fso.OpenTextFile("C:\FileLock.txt", ForReading)
    Dim LockBit As Integer
        LockBit = FileLock.ReadAll
    FileLock.Close

    'If the bit is 1 (file in use) then wait 1 second and try again (up to 10 times)
    For try = 1 To 10
        If LockBit = 1 Then
            Application.Wait (Now + TimeValue("0:00:1"))
            Set FileLock = fso.OpenTextFile("C:\FileLock.txt", ForReading)
            LockBit = FileLock.ReadAll
            FileLock.Close
        Else: GoTo Line1 'when the bit is 0 (file available)
        End If
        If try = 10 Then
            MsgBox "File not available"
            Exit Sub
        End If
    Next try

Line1:
    Call LockTheFile(fso, True) 'Change the lock bit to "1" to show the file's in use
    Set WriteFile = fso.OpenTextFile("C:\WriteFile.txt", ForWriting)
        'Do what you will with the file
        MsgBox "Write Successful"
    WriteFile.Close
    Call LockTheFile(fso, False) 'Change the lock bit to "0" to show the file's available

End Sub
我将此子代码分离以使主代码更加精简

Sub LockTheFile(fso, SetLock As Boolean)
    'Write "1" to a lock file to indicate the text file is in use, or "0" to indicate not in use
    Set BitFile = fso.CreateTextFile("C:\FileLock.txt", True)

    If SetLock = True Then
        BitFile.WriteLine "1"
    Else
        BitFile.WriteLine "0"
    End If

    BitFile.Close
End Sub

请编辑您的问题并正确设置格式。事实上,我不知道你在问什么。你的问题是什么?我想使用vba脚本检查文本文件是否打开。你发布的代码,它的作用是什么。它失败了吗?是的,不管文件是打开还是关闭,它都会给出相同的消息。很高兴听到这个消息!