Home » excel » excel – Need to reset an autosave timer when the document is saved manually

excel – Need to reset an autosave timer when the document is saved manually

Posted by: admin May 14, 2020 Leave a comment

Questions:

So, I have some code autosaving my Excel project. I am looking for a way to stop / reset the autosave timer when the document is saved manually. This is what I am using for the autosave.

If ThisWorkbook.Name = "mydoc.xlsm" Then
    Application.OnTime Now + TimeValue("00:30:00"), "Save_Workbook"
End If

I tried an if statement:

If ActiveWorkbook.Saved = False Then
    Application.OnTime Now + TimeValue("00:01:00"), "ShowMsg"
End If

To see if by saving the document I would stop ShowMsg from going off. But even after saving the document, ShowMsg goes off at the 1 minute mark. It doesn’t change the timer.

Edit: Solution I used

I was able to accomplish what I wanted using the following methods.

In the Workbook

I used the function Workbook_BeforeSave() in order to stop the timer after being saved.

Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, _
    Cancel As Boolean)

    Call StopTimer

End Sub

This is calling the Module StopTimer(). Which is defined on the Module level.

Option Explicit
Public RunWhen
Public cRunWhat

Public Sub StartTimer()
    RunWhen = Now + TimeValue("00:01:00")
    cRunWhat = "Save"
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub
Public Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
    StartTimer
End Sub

Now, I had a different function in the workbook calling StartTimer, and Save() was just a basic save the workbook function.

A main issue I was having was solved in another post, by me. Linked here

I will be copying this solution to that post.

How to&Answers:

You could use the BeforeSave event.

https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.beforesave

Something like:

In ThisWorkbook:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _ 
        Cancel as Boolean) 
Call Timer
End Sub

In Module:

Sub Timer()
Application.OnTime Now + TimeValue("00:00:03"), "Hello"
End Sub
Sub Hello()
MsgBox "Hello"
End Sub