如何从Visual Basic了解Excel文件的打开/关闭状态

时间:2011-05-03 17:14:16

标签: file-io vb6

我在Visual Basic 6中工作。我从vb6命令打开了一个Excel文件,然后将其关闭。现在我想获取文件状态。怎么知道该文件已关闭或打开?请给我从Visual Basic 6获取某个文件的打开或关闭状态的syntex。或者换句话说,我通过vb santax打开了一个excel文件然后再打开同一个文件然后应该响应这个文件已经开放,请选择另一个。

1 个答案:

答案 0 :(得分:3)

this

怎么样?
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