Solved

Application.ontime is running even after Excel workbook is closed

Posted on 2012-04-04
4
397 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now