Hi Team,
I am using below function , to check whether there is already Open workbook , but its not working.
Also what is the meaning of below lines not getting can you add comment it. or Any alternate code check open file.
Thanks
mg
I am using below function , to check whether there is already Open workbook , but its not working.
Also what is the meaning of below lines not getting can you add comment it. or Any alternate code check open file.
VBA Code:
On Error Resume Next
fileID = FreeFile()az9w
Open FileName For Input Lock Read As #fileID
Close fileID
errNum = Err
On Error GoTo 0
Sub test()
Dim wbk2 As Workbook
Dim str As String
str = "E:\VBA\009 - Enter Time in Cell.xlsm"
Dim wbk As Workbook
Set wbk = OpenWorkbook(str, False, True)
If wbk Is Nothing Then
GoTo myend
End If
myend:
On Error Resume Next
wbk.Close False
wbk2.Close False
Set wbk = Nothing
On Error GoTo 0
End Sub
Function OpenWorkbook(ByVal sFilename As String, ByVal updatelinks As Boolean, ByVal ReadOnly As Boolean) As Workbook
On Error GoTo eh
'Check file exists
If (Dir(sFilename) <> "") Then
'if workbook is already open inform the user
If IsWorkBookOpen(sFilename) Then
MsgBox ("The workbook[" + sFilename + " ] is already open by user [" + Application.UserName + "].Please close the file and run Again.!")
GoTo Done
End If
Set OpenWorkbook = Workbooks.Open(sFilename, updatelinks, ReadOnly)
Else
MsgBox ("The workbook " & sFilename & " Could not be found.")
End If
Done:
Exit Function
eh:
MsgBox Err.Description
End Function
Function IsWorkBookOpen(FileName As String)
Dim fileID As Long
Dim errNum As Long
'Open file and Check for Error
On Error Resume Next
fileID = FreeFile() ' from below how this function works can you add comment.
Open FileName For Input Lock Read As #fileID
Close fileID
errNum = Err
On Error GoTo 0
End Function
Thanks
mg