Hello,
There are a number of ways of doing this. One way is like this:-
Sub IsItOpen()
Dim strFileName As String, wbEnum As Workbook, blnIsOpen As Boolean
strFileName = "C:\temp\Mybook.xls"
For Each wbEnum In Excel.Workbooks
If wbEnum.Path & "\" & wbEnum.Name = strFileName Then blnIsOpen = True: Exit For
Next
If blnIsOpen = False Then 'Workbook isn't open
Workbooks.Open strFileName
End If
End Sub
HTH,
D
Re: Oops, one small change needed
Because the comparison will be case sensitive (unless you have Option Compare Text at the top of the module) you should change the code to this:-
Sub IsItOpen()
Dim strFileName As String, wbEnum As Workbook, blnIsOpen As Boolean
strFileName = "C:\Temp\Mybook.xls"
For Each wbEnum In Excel.Workbooks
If UCase(wbEnum.Path & "\" & wbEnum.Name) = UCase(strFileName) Then blnIsOpen = True: Exit For
Next
If blnIsOpen = False Then 'Workbook isn't open
Workbooks.Open strFileName
End If
End Sub
It just makes the file you're looking for and the name of each workbook UPPER case.
Regards,
D
Re: Oops, one small change needed
Thanks, youre a champion!
Re: Oops, one small change needed
How about this UDF ?
Public Function IsBookOpen(BName As String) As Boolean
Dim WBk As Workbook
On Error Resume Next
If InStr(1, BName, ".xls", 1) = 0 Then BName = BName & ".xls"
Set WBk = Workbooks(BName)
IsBookOpen = (Err = 0) + 0
Set WBk = Nothing
End Function
Juan Pablo G.