1

It appears that if the OnTime event is registered by a programmatic MyBook.Close statement, then OnTime never runs.

This code works fine:

Sub TestOnTime()
          Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub


Sub MySub()
          Debug.Print "hello"
End Sub

Run TestOnTime. MySub will execute, as expected.


And this code runs fine:

ThisWorkbook:

Dim WithEvents oApp As Application


Private Sub Workbook_Open()
          Set oApp = Application
End Sub


Private Sub oApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
          Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub

Module 1:

Sub MySub()
          Debug.Print "hello"
End Sub
  1. Manually close another workbook to fire oApp_WorkbookBeforeClose.
  2. MySub executes, as expected.

But this code fails. The OnTime event never runs.

Book 1

ThisWorkbook:

Dim WithEvents oApp As Application


Private Sub Workbook_Open()
          Set oApp = Application
End Sub


Private Sub oApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
          Application.OnTime Now + TimeValue("00:00:05"), "MySub"
End Sub

Module 1:

Sub MySub()
          Debug.Print "hello"
End Sub

Book 2

Module 1:

Sub Test()
          ThisWorkbook.Close
End Sub
  1. Run Test to close Book 2.
  2. Book 1 oApp_WorkbookBeforeClose executes, as expected.
  3. But the Book 1 MySub event never runs.

Why?

Why doesn't OnTime execute if registered by a Workbook_BeforeClose event? No code is running in the book that's closing. OnTime works no problem with other events (eg programmatically opening a workbook). Somehow, closing a workbook programmatically breaks OnTime. Why?

johny why
  • 2,047
  • 7
  • 27
  • 52

1 Answers1

1

As Book 2 is being closed, You should include the Application.OnTime procedure in Book 2 and not in Book 1

Also, I think those books should be saved once and not new books.

Sub test()
Application.OnTime Now + TimeValue("00:00:05"), "Book 1.xlsm!MySub"
ThisWorkbook.Close
End Sub

EDIT Jul 6 -

You are closing the workbook and then you are trying to run a macro MySub in the same workbook after 5 seconds. Macro in the same workbook will not run once the book is closed. Application will reopen the file to run the macro. If you want to close Book2 after 5 seconds of closing Thisworkbook then --

in Thisworkbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime Now + TimeValue("00:00:05"), "Book2.xlsm!Test"
End Sub

So, after closing Thisworkbook, macro named "Test" in Book2 will run and will close that workbook.

Naresh
  • 2,984
  • 2
  • 9
  • 15
  • You have already posted this question [here](https://stackoverflow.com/questions/68217359/workbook-closed-by-vba-doesnt-close-referenced-isaddin-workbook) in a different way. I would still suggest if you are programmatically closing the client.xlsb then try closing the addin book first in the same procedure in client.xls – Naresh Jul 03 '21 at 04:51
  • 1
    This question is about one possible solution to the other question. I believe it's not possible to close the addin book first: "This workbook is currently referenced by another workbook and cannot be closed." – johny why Jul 03 '21 at 05:04
  • @johnywhy May be you have to remove the reference first in client.xlsb and then close the addin and then close client.xlsb all programmatically . – Naresh Jul 03 '21 at 05:14
  • 1
    It's not possible to programmatically remove references without trusted access to VBA, which can only be done manually. We can't do that. – johny why Jul 03 '21 at 05:47
  • 1
    i improved on your answer here, to make a solution to the other question. https://stackoverflow.com/a/68236858/209942 – johny why Jul 03 '21 at 13:51
  • Why, tho', doesn't the OnTime execute? OnTime works with other events no problem (eg Workbook_Open) Somehow, closing a workbook breaks OnTime. – johny why Jul 06 '21 at 12:54
  • Didn't understand your edit. In my OP, no code is running in the book that's closing. – johny why Jul 07 '21 at 03:57
  • In `Thisworkbook` you are using the event `oApp_WorkbookBeforeClose` and in the same workbook `Module1` you are trying to run `MySub` 5 seconds after `Thisworkbook` is closed with `Application.OnTime Now + TimeValue("00:00:05"), "MySub"` .. So `MySub` will not run once `ThisWorkbook` is closed. Instead of referring to `MySub` in `OnTime` statement you can refer to some macro in other open workbook or addin. But for that also to be executed, excel needs to be running – Naresh Jul 07 '21 at 04:02
  • i think you're describing what i'm doing in my solution here https://stackoverflow.com/a/68236858/209942 – johny why Jul 07 '21 at 04:09
  • True. But in the solution i linked, the sub called by OnTime isn't in the workbook being closed. Thx! – johny why Jul 07 '21 at 04:13
  • Oh you want me to see the other link? I will check it. – Naresh Jul 07 '21 at 04:16
  • 1
    "I think those books should be saved" -- i haven't found it necessary to save the closing book for your wrapper to work. Works fine with new, never-saved books. – johny why Jul 07 '21 at 11:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234616/discussion-between-naresh-and-johny-why). – Naresh Jul 07 '21 at 11:50