so i'm trying write timer class in vba, , seems except 1 thing.
when call application.ontime required provide recurring function's name, , function sits inside timer class hence dont know how access it.
i have tried passing timer instance name timer call function follows:
application.ontime pnexttick, pname & ".restartinterval" where pname name of timer instance outside:
set autoupdater = new c_timer autoupdater.name = "autoupdater" but whatever do, receive following error:
cannot run macro "blablablablablabla.xlsm'!autoupdater.restartinterval'. macro may not available in workbook or macros may disabled.
macros not disabled in case, need here...
here c_timer class far:
'test module public sub test() set autoupdater = new c_timer autoupdater.name = "autoupdater" autoupdater.interval = "00.00.5" autoupdater.whattorun = "dosomething" autoupdater.starttimer end sub function dosomething() msgbox "sdoiigsligsdgoidjh" end function 'c_timer class private pwhattorun string private pinterval string private pname string private prunning boolean private pnexttick private function restartinterval() if pwhattorun <> "" application.run pwhattorun pnexttick = + timevalue(pinterval) application.ontime pnexttick, pname & ".restartinterval" end if end function public function starttimer() boolean on error goto hell if timevalue(pinterval) > timevalue("00.00.00") , pname <> "" , prunning <> true pnexttick = + timevalue(pinterval) application.ontime pnexttick, pname & ".restartinterval" prunning = true else goto hell end if exit function hell: prunning = false m_settings.setstatus "failed update" end function public function stoptimer() boolean on error goto hell if prunning = true application.ontime pnexttick, "restartinterval", , false prunning = false else goto hell end if exit function hell: end function public property whattorun() string whattorun = pwhattorun end property public property let whattorun(value string) pwhattorun = value end property public property interval() string interval = pinterval end property public property let interval(value string) pinterval = value end property public property name() string name = pname end property public property let name(value string) pname = value end property update: ended using answer below. here full code if wants use in future:
'm_factory module public function createtimer(name string, interval string) c_timer set newtimer_ = new c_timer newtimer_.name = name newtimer_.interval = interval set createtimer = newtimer_ end function 'c_timer class private pinterval string private pname string private prunning boolean private pnexttick public function process(func) if func <> "" application.run func pnexttick = + timevalue(pinterval) application.ontime pnexttick, pname & "_tick" end if end function public function starttimer() boolean on error goto hell if timevalue(pinterval) > timevalue("00.00.00") , pname <> "" , prunning <> true pnexttick = + timevalue(pinterval) application.ontime pnexttick, pname & "_tick" prunning = true else goto hell end if exit function hell: prunning = false m_settings.setstatus "failed update, close & reopen document" end function public property start() c_timer starttimer set start = me end property public function stoptimer() boolean on error goto hell if prunning = true application.ontime pnexttick, pname & "_tick", , false prunning = false else goto hell end if exit function hell: end function public property interval() string interval = pinterval end property public property let interval(value string) pinterval = value end property public property name() string name = pname end property public property let name(value string) pname = value end property 'test module public timer1 c_timer, timer2 c_timer public sub test() set timer1 = m_factory.createtimer("timer1", "00.00.01").start set timer2 = m_factory.createtimer("timer2", "00.00.5").start end sub public function timer1_tick() timer1.process "dosomething" end function public function timer2_tick() timer2.process "domorestuff" end function public sub stopit() timer1.stoptimer timer2.stoptimer end sub function dosomething() sheets(1).cells(1, 1).value = format(datetime.now, "hh:nn:ss") end function function domorestuff() sheets(1).cells(1, 2).value = format(datetime.now, "hh:nn:ss") end function
the name of procedure run ontime should public sub standard module. have tried following?
standard module
set autoupdater = new c_timer public sub triggerupdater() autoupdater.insidemytimerclass end sub class c_timer
application.ontime pnexttick, "triggerupdater" public function insidemytimerclass() variant ... end function
Comments
Post a Comment