Dharmendra
Dharmendra

Reputation: 129

How to check if another workbook is open?

I open a workbook by using TASK MANAGER. Sometimes the previous task is not completed i.e. the macro is already running in another workbook.

I want to check if any other workbook is already open or running macro; close current opened workbook by task manager.

I have simple code as per below:

If thisworkbook.name <> activeworkbook.name then
    call sendemail ' don't need code
else
    Call Run_Dashboard
End if

Upvotes: 0

Views: 299

Answers (2)

Patrick Lepelletier
Patrick Lepelletier

Reputation: 1652

Function Is_Workbook_Open(byval WB_Name as String) as Boolean
Dim WB as Workbook
For each WB in Application.Workbooks
    if WB.Name = WB_Name then
        Workbook_Open = True
        Exit Function
    End if
Next WB
End Function

Answers True if the Workbook is opened, no error handling.

Upvotes: 0

user1599615
user1599615

Reputation:

Dim wb as Workbook
On Error Resume Next                       '//this is VBA way of saying "try"'
Set wb = Application.Workbooks(wbookName)
If err.Number = 9 then                     '//this is VBA way of saying "catch"'
    'the file is not opened...'
End If

Upvotes: 1

Related Questions