简体   繁体   中英

How to check if another workbook is open?

I open a workbook by using TASK MANAGER. Sometimes the previous task is not completed ie 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
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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM