簡體   English   中英

用於檢查文本文件是否打開的 VBA 腳本

[英]VBA Script to check if text file is open or not

我正在檢查文件是否打開,即 .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

事實證明,使用記事本打開 .txt 時不會鎖定文件,因此無法知道 .txt 文件是否打開。 因此,如果該 .txt 文件在 Wordpad 或 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM