Solved

Application.ontime is running even after Excel workbook is closed

Posted on 2012-04-04
4
409 Views
Last Modified: 2012-04-04
I have an excel 2007 workbook. I need to automatically close it after 5 minutes of opening it. I had written  a code as under and this works

Private Sub Workbook_Open()
Application.OnTime Now() + TimeValue("00:05:00"), "Autoclose"
End Sub


Sub Autoclose()

ThisWorkbook.Save
ThisWorkbook.Close

End Sub

However, in case I close this Excel workbook after 2 minutes of opening it, the Excel application is still opening the closed workbook and then again closing it.
This should not happen. The excel application should close only if the Excel application is manually opened by me. Please suggest a method / VBA code.
0
Comment
Question by:vsuripeddi
  • 3
4 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 300 total points
ID: 37807752
You have to kill the timer before you close the workbook. Store the start time in a variable and then use OnTime to kill it:

   On Error Resume Next
   Application.OnTime mTimerTime , "Autoclose", Schedule:=False
   On Error GoTo 0

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 37807758
Place the above code in the Workbook_BeforeClose event handler.

Kevin
0
 

Author Comment

by:vsuripeddi
ID: 37807829
What should be the value of mTimerTime? Can I still use
mTimerTime = Application.OnTime Now() + TimeValue("00:05:00")   ?
Or should mTimerTime be of a different value?
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 37807838
You want to set mTimerTime to whatever time you want the timer to pop. Then use that same value to kill the timer should you close the workbook before the timer can pop.

Kevin
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question