The argument is

Bk—The name of the workbook being checked.

Function BookOpen(Bk As String) As Boolean Dim T As Excel.Workbook Err.Clear 'clears any errors

On Error Resume Next 'if the code runs into an error, it skips it and continues Set T = Application.Workbooks(Bk) BookOpen = Not T Is Nothing

'If the workbook is open, then T will hold the workbook object and therefore

'will NOT be Nothing


On Error GoTo 0

End Function

Here is an example of using the function:

Sub OpenAWorkbook()

Dim IsOpen As Boolean

Dim BookName As String

BookName = "Chapter 4 samples.xls"

IsOpen = BookOpen(BookName) 'calling our function - don't forget the parameter If IsOpen Then

MsgBox BookName & " is already open!"


Workbooks.Open (BookName) End If End Sub

0 0

Post a comment