How can I know the open/closed status of an Excel file from Visual Basic
I'm working in Visual Basic 6. I opened an Excel file from vb6 开发者_如何学编程command and then closed it. Now I want to get the file status. How can know that file is closed or opened? Please give me the syntex of getting the open or closed status of a certain file from Visual Basic 6. OR in other words, i opened a file of excel through vb santax then again i open the same file then there should be response that this file is already opended please select another one.
How about 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
精彩评论