简体   繁体   English

如何检查另一个工作簿是否打开?

[英]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. 如果打开了工作簿,则为True,没有错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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