excel vba - Stop Save File event by using another command button -
am new in vb. have excel file auto saving in intervals. want stop auto saving function using command button... plz me ... code ...
private sub autosave_cmd_click()
dtime = time + timevalue("00:00:05") 'set time per requirement
with application
.ontime dtime, "autosaveas" .enableevents = false .displayalerts = false thisworkbook.saveas "c:\<my file path>\auto_save_macro.xls" .enableevents = true application.enableevents = false
end with
end sub
now question code stop auto save function through "stop_auto_save_cmd" command button? please me ... in advance ...
private sub stop_auto_save_cmd_click()
'what code here?
end sub
your code snippet looks lot code found here - difference being think code @ link works, , yours doesn't.
when describe how "working" code works, should able figure out why yours doesn't:
public dtime date sub autosaveas() dtime = time + timevalue("00:05:00") application .ontime dtime, "autosaveas" .enableevents = false .displayalerts = false thisworkbook.saveas "filepath&namehere(no .xls)" .enableevents = true end end sub
here see public
variable dtime
declared @ module level; set sensible time (now + 5 minutes) inside routine called autosaveas()
, instructs application
(i.e., excel) execute call autosaveas
when timer runs out (in example, every 5 minutes). after setting "call me again in 5 minutes" command, performs actual saveas, alerts disabled.
to work need call autosaveas
first time; after that, "call itself" because timer go off every 5 minutes (it's function keeps hitting own snooze button, if like). can done in code associated button that's clicked "autosave on" functionality (or autoopen event - depends on behavior want):
private sub autosaveon_click() autosaveas end sub
and finally, need "turn off" event loop (instead of "snooze", hit "off" on alarm clock). put in handler ("off") button:
private sub autosaveoff_click() application.ontime dtime, "autosaveas", , false end sub
the fourth parameter, false
, says "unschedule this". turns off "infinite loop" started first call autosaveas
. unplugs alarm clock, if like.
to complete, must add handler when file closed - because timer keep running if close workbook leave excel open, , give strange , annoying error message. need this:
private sub workbook_beforeclose(cancel boolean) application.ontime dtime, "autosaveas", , false end sub
note - lightly adapted code link gave above, , want give credit original author (dave hawley) example. did show how interpret (and similarity code have, think have seen same site, or @ least copy of copy of site...)
Comments
Post a Comment