Solved

Application.ontime is running even after Excel workbook is closed

Posted on 2012-04-04
4
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

687 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