Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application.ontime is running even after Excel workbook is closed

Status
Not open for further replies.

dasiga444

MIS
Jan 27, 2012
5
US
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.

 
Use the workbook-close event to cancel.


When you run OnTime with a schedule of False, you must specify the exact same run time as when it was last launched. Best to calculate the time 2 mins hence and store that in a global variable when launching, then use that same variable when stopping.

Gavin
 
If nothing else is open in Excel try adding Application.Quit inplace of thisworkbook.close

In a worst case scenario, open the workbook in its own excel session to be able to quit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top