File io 如何从Visual Basic中知道Excel文件的打开/关闭状态

File io 如何从Visual Basic中知道Excel文件的打开/关闭状态,file-io,vb6,File Io,Vb6,我在Visual Basic 6中工作。我使用vb6命令打开了一个Excel文件,然后将其关闭。现在我想获取文件状态。如何知道该文件已关闭或打开?请告诉我从Visual Basic 6获取某个文件的打开或关闭状态的语法。或者换句话说,我通过vb santax打开了一个excel文件,然后再次打开同一个文件,应该会有响应,该文件已打开。请选择另一个。如何: Option Explicit Private Function FileStatus(ByVal FileName As String)

我在Visual Basic 6中工作。我使用vb6命令打开了一个Excel文件,然后将其关闭。现在我想获取文件状态。如何知道该文件已关闭或打开?请告诉我从Visual Basic 6获取某个文件的打开或关闭状态的语法。或者换句话说,我通过vb santax打开了一个excel文件,然后再次打开同一个文件,应该会有响应,该文件已打开。请选择另一个。

如何:

Option Explicit

Private Function FileStatus(ByVal FileName As String) As VbTriState
    Dim intFile As Integer

    On Error Resume Next
    GetAttr FileName
    If Err.Number Then
        FileStatus = vbUseDefault 'File doesn't exist or file server not available.
    Else
        Err.Clear
        intFile = FreeFile(0)
        Open FileName For Binary Lock Read Write As #intFile
        If Err.Number Then
            FileStatus = vbFalse 'File already open.
        Else
            Close #intFile
            FileStatus = vbTrue 'File available and not open by anyone.
        End If
    End If
End Function

Private Sub cmdGetStatus_Click()
    Select Case FileStatus(txtFileName.Text)
        Case vbUseDefault
            MsgBox "File doesn't exist or file server not available"
        Case vbFalse
            MsgBox "File already open"
        Case vbTrue
            MsgBox "File available and not open by anyone"
    End Select
End Sub