I whould like to check on a particular file in a share network whether it is open by other user. If it is open by other user, i would like it to be close. Here is my code for opening the excel file and checking code. I have no idea on how to close it.
Button click
Private Sub Command1_Click()
Dim tempXlb As Excel.Application
Dim xlb As Excel.Workbook
If IsFileOpen(App.Path & "\abc.xls") Then
MsgBox "file already open, terminating"
End If
Set xlb = GetObject(App.Path & "\abc.xls")
xlb.Activate
xlb.Worksheets(1).Cells(1, 1).Value = Text1.Text
xlb.Windows(1).Visible = True
xlb.Windows.Application.Visible = True
End Sub
Module
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function
Please help me to close the excel there are being open by other user. thx