From this link I've found this code :
Option Explicit
Sub Sample()
Dim Ret
Ret = IsWorkBookOpen("C:\myWork.xlsx")
If Ret = True Then
MsgBox "File is open"
Else
MsgBox "File is Closed"
End If
End Sub
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
But for me, the question in that link is not clear. I'm unable to know whether the question is asking about the workbook is already open in the same computer, or if the workbook is already open by another computer where both computers connected to the same network.
I'm imagining something like this :
Computer-A and Computer-B connected to the same network.
Comp-A make a shared folder which has the excel file, say test.xlsm,
so comp-B can open test.xlsm from Comp-A shared folder.
Since I don't have two computers and the network... I wonder if the case something like this :
Comp-B open the test.xlsm as read only mode
While test.xlsm being opened by Comp-B as read only mode,
later on Comp-A open BLA.xlsm which has macro to open test.xlsm,
so it check first with the sub/function above before opening it.
My question :
will Open FileName For Input Lock Read As #ff throw an error ?
why i ask, because I'm not sure if the sub/function above is meant for "comp-B open test.xlsm as normal mode" only, or it also meant for : "comp-B open test.xlsm as readonly mode"
Any kind of response will be greatly appreciated. Thank you in advanced.
The function IsWorkBookOpen() simply checks whether a workbook is already opened by another process. If so, it returns True. Otherwise, it returns False.
When opening a file using the Open statement, specifying the access type as
Lock Readis not allowed if the file is already opened by another process. Otherwsie, an error occurs.Have a look at the following Microsoft reference...
Open Statement