<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer FAQs &#38; Tips &#187; Auto-Timer</title>
	<atom:link href="http://www.computer-faqs.com/tag/auto-timer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.computer-faqs.com</link>
	<description>Answers to your Questions, Tips to enhance your Experience</description>
	<lastBuildDate>Sun, 16 Aug 2009 23:16:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>VBA Auto Executor</title>
		<link>http://www.computer-faqs.com/2008/08/24/vba-auto-executor/</link>
		<comments>http://www.computer-faqs.com/2008/08/24/vba-auto-executor/#comments</comments>
		<pubDate>Sun, 24 Aug 2008 03:45:00 +0000</pubDate>
		<dc:creator>Manet</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Auto-Timer]]></category>
		<category><![CDATA[Time-Interval]]></category>
		<category><![CDATA[VB-VBA]]></category>

		<guid isPermaLink="false">http://compfaqs.acpiwat.com/?p=37</guid>
		<description><![CDATA[Sometimes when you need to execute a task every period of time and there&#8217;s no straight predefined function that you can just call and use with a few lines of codes. You will actually need to implement your own module. I&#8217;ve written this nice class module sometime ago and I believe it will be useful [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when you need to execute a task every period of time and there&#8217;s no straight predefined function that you can just call and use with a few lines of codes. You will actually need to implement your own module.</p>
<p>I&#8217;ve written this nice class module sometime ago and I believe it will be useful for other VB/VBA developer out there. This class is an Auto Executor, it will automatically call and run a defined Routine.</p>
<p>It will solve the problem in the scenario above and it is written in an object oriented style which you can just embedded the whole class in your project and when you need to use it, just create the instance of it then call the public sub routine to perform the task.</p>
<p><span id="more-37"></span></p>
<h3>How to use it</h3>
<ul>
<li>Create a Class module in your VB/VBA project call it <em>MacroAutoX</em></li>
<li>Copy and paste the source code from here</li>
<li>See <em>Completed Usage Example</em> below</li>
<li>When you want to end the job, just call <em>EndXRoutine</em> to end the task</li>
</ul>
<p>You also need to understand CallByName Function</p>
<blockquote class="codefunction"><p>CallByName(object, procname, calltype,[args()])</p></blockquote>
<p>where</p>
<ul>
<li><strong>object</strong> &#8211; Required; Variant (Object). The name of the object on which the function will be executed.</li>
<li><strong>procname</strong> &#8211; Required; Variant (String). A string expression containing the name of a property or method of the object.</li>
<li><strong>calltype</strong> &#8211; Required; Constant. A constant of type vbCallType representing the type of procedure being called.</li>
<li><strong>args()</strong> &#8211; Optional: Variant (Array).</li>
</ul>
<p>See Visual Basic Help if you need detailed explanation. You can press F1 while cursor is on keyword.</p>
<p><strong>Completed Usage Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> MyCallBackRoutine() <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
    CallByName Me, <span style="color: #800000;">&quot;MyMainJobSubroutine&quot;</span>, VbMethod
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Function</span> AutoMyCallBackRoutine(<span style="color: #000080;">ByVal</span> EventID <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, _
                                       <span style="color: #000080;">ByVal</span> SystemTime <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
    UF_MyForm.MyCallBackRoutine
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> SetAutoTask(Interval <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>)
<span style="color: #008000;">' Call this Sub whenever you need to start your auto task
</span>    <span style="color: #000080;">Dim</span> iError <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
    <span style="color: #000080;">Dim</span> MAutoX <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> MacroAutoX
    MAutoX.RemoteRountine = RoutineVal(<span style="color: #000080;">AddressOf</span> AutoMyCallBackRoutine)
    MAutoX.TimeInterval = 60 * MAutoX.ONESECOND
    iError = MAutoX.StartXRoutine
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></pre></div></div>

<h3>Source Code</h3>
<p>Calss Module: MacroAutoX</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' --------------------------------------------------------------------------------------------
</span><span style="color: #008000;">' Class:        MacroAutoX
</span><span style="color: #008000;">' Description:  This class is an Auto Executor. It will automatically call and run Routine
</span><span style="color: #008000;">'               defined in Property &quot;RemoteRountine&quot;
</span><span style="color: #008000;">'
</span><span style="color: #008000;">' Author:       Manet Yim ( manet.yim at gmail dot com )
</span><span style="color: #008000;">' Date Create:  23 Jun 2007
</span><span style="color: #008000;">' Version:      1.0
</span><span style="color: #008000;">' --------------------------------------------------------------------------------------------
</span>
<span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Declare</span> <span style="color: #000080;">Function</span> FindWindow <span style="color: #000080;">Lib</span> <span style="color: #800000;">&quot;user32&quot;</span> <span style="color: #000080;">Alias</span> <span style="color: #800000;">&quot;FindWindowA&quot;</span> _
   (<span style="color: #000080;">ByVal</span> lpClassName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> lpWindowName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Declare</span> <span style="color: #000080;">Function</span> SetTimer <span style="color: #000080;">Lib</span> <span style="color: #800000;">&quot;user32&quot;</span> _
   (<span style="color: #000080;">ByVal</span> hWnd <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, <span style="color: #000080;">ByVal</span> nIDEvent <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, <span style="color: #000080;">ByVal</span> uElapse <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, <span style="color: #000080;">ByVal</span> lpTimerFunc <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
&nbsp;
<span style="color: #000080;">Private</span> <span style="color: #000080;">Declare</span> <span style="color: #000080;">Function</span> KillTimer <span style="color: #000080;">Lib</span> <span style="color: #800000;">&quot;user32&quot;</span> _
   (<span style="color: #000080;">ByVal</span> hWnd <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, <span style="color: #000080;">ByVal</span> nIDEvent <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
&nbsp;
<span style="color: #008000;">' Window Class name for &quot;Microsoft Office Word&quot; is OpusApp
</span><span style="color: #000080;">Private</span> <span style="color: #000080;">Const</span> C_WINDOW_CLASSNAME = <span style="color: #800000;">&quot;OpusApp&quot;</span>
&nbsp;
<span style="color: #008000;">' Time in different units
</span><span style="color: #000080;">Private</span> <span style="color: #000080;">Const</span> ONE_SECOND <span style="color: #000080;">As</span> <span style="color: #000080;">Single</span> = 1000
<span style="color: #000080;">Private</span> <span style="color: #000080;">Const</span> ONE_MINUTE <span style="color: #000080;">As</span> <span style="color: #000080;">Single</span> = ONE_SECOND * 60
<span style="color: #000080;">Private</span> <span style="color: #000080;">Const</span> ONE_HOUR <span style="color: #000080;">As</span> <span style="color: #000080;">Single</span> = ONE_MINUTE * 60
&nbsp;
<span style="color: #008000;">' Remote Routine to be called and execute
</span><span style="color: #000080;">Private</span> RemoteCallBackRountine <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
<span style="color: #008000;">' Time interval for auto execution
</span><span style="color: #000080;">Private</span> AutoTimeInterval <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
&nbsp;
<span style="color: #008000;">' ID of timer at runtime
</span><span style="color: #000080;">Public</span> TimerID <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Get</span> ONESECOND()
    ONESECOND = ONE_SECOND
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Get</span> ONEMINUTE()
    ONEMINUTE = ONE_MINUTE
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Get</span> ONEHOUR()
    ONEHOUR = ONE_HOUR
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Let</span> RemoteRountine(RRoutine <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>)
    RemoteCallBackRountine = RRoutine
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Get</span> RemoteRountine() <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
    RemoteRountine = RemoteCallBackRountine
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Let</span> TimeInterval(Interval <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>)
    AutoTimeInterval = Interval
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Property</span> <span style="color: #000080;">Get</span> TimeInterval() <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
    TimeInterval = AutoTimeInterval
<span style="color: #000080;">End</span> <span style="color: #000080;">Property</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> StartXRoutine() <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span>
<span style="color: #008000;">' Start executing the rountine by intitiating the Timer.
</span><span style="color: #008000;">' It will run every &quot;Interval&quot; time sepcified.
</span><span style="color: #008000;">' Interval time to be specified in number of Second
</span>    TimerID = SetTimer(hWnd:=FindWindow(C_WINDOW_CLASSNAME, Application.Caption), _
                nIDEvent:=0, uElapse:=TimeInterval, lpTimerFunc:=RemoteCallBackRountine)
&nbsp;
Finally:
    <span style="color: #008000;">' If error occur during starting the time and routine, return the Err Object
</span>    <span style="color: #008000;">' otherwise return Nothing
</span>    StartXRoutine = Err.number
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
<span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> EndXRoutine()
<span style="color: #008000;">' Ending the execution of routine by terminating the timer with reference to TimerID
</span>    <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span>
    KillTimer hWnd:=FindWindow(C_WINDOW_CLASSNAME, Application.Caption), nIDEvent:=TimerID
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></pre></div></div>

<p>Download this class: <a href="http://www.computer-faqs.com/wp-content/uploads/2008/08/macroautox.cls">macroautox.cls</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.computer-faqs.com/2008/08/24/vba-auto-executor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
