Excel

Customizing AutoSave Programmatically

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Paleo

Description:

Make sure your user is gonna save his/her workbook after a certain amount of time. 

Discussion:

Sometimes we want to assure the user will have his/her workbook saved at a certain amount of time regardless his/her autosave settings. Save the workbook as a template for the functionality to apply to new workbooks based on the template. Adjust the TimeOut value to suit your users. 

Code:

instructions for use

			

'//Insert the following in the Workbook module Private Sub Workbook_Open() Call Tempo End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) Call Limpa End Sub '//Insert the following in a standard module Public vartimer As Variant Const TimeOut = 5 'in minutes Sub Salva() ActiveWorkbook.Save Call Tempo End Sub Sub Tempo() vartimer = Format(Now + TimeSerial(0, TimeOut, 0), "hh:mm:ss") If vartimer = "" Then Exit Sub Application.OnTime TimeValue(vartimer), "Salva" End Sub Sub Limpa() On Error Resume Next Application.OnTime earliesttime:=vartimer, _ procedure:="Salva", schedule:=False On Error GoTo 0 End Sub

How to use:

  1. Copy the Private Sub Workbook_Open and the Private Sub Workbook_BeforeClose codes
  2. Open any workbook.
  3. Press Alt + F11 to open the Visual Basic Editor (VBE).
  4. Press Ctrl + R to show the Project Explorer.
  5. Double-click This_Workbook
  6. Paste the Private Subs into the right-hand code window.
  7. Click Insert from the VBE Command Bar and select Module
  8. Double-click the new module
  9. Copy the rest of the code
  10. Paste them on the new module
  11. Close the VBE and save the file
  12. Close the file
  13. Re-Open the file
 

Test the code:

  1. Open a file with the above code and you will see it working (the example saves the workbook every 5 minutes)
 

Sample File:

SavingWorkbook.zip 8.51KB 

Approved by mdmackillop


This entry has been viewed 297 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express