Solved

Timer for macro...

Posted on 2002-05-14
10
270 Views
Last Modified: 2010-05-02

Hi,
  It's very simple question ..... as i am not a VB developer..... it became hard to me.....

 I want a Timer.. which will keep calling my one macro in MS-Word after some time interval. Is it possible, If yes.. can u pass me the code.....

thanks,
Shaan
0
Comment
Question by:shahnazali
  • 6
  • 4
10 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7009714
Well, go to VBA IDE (Alt+F11)
Go to Project panel a do a right click
Select Insert, Module
Paste this code in the code pane window:

Public Sub TimerEvent()
MsgBox "put the action you want to satrt here"
End Sub

Do a double click on ThisDocument and paste this code:

Private Sub Document_Open()
Application.OnTime Now() + TimeValue("00:00:15"), "TimerEvent"
End Sub



0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7009719
oops!, sorry, save the document, close it and open again.
After 15 seconds from doc is opened, you would see a message box with the text entered.
In timervenet you have to write wath you want to perform.
If you have a macro already, replace TimerEvent with the name of your macro. replace interval at your wishes also.

Application.OnTime Now() + TimeValue("00:00:15"), "MyMacro"
0
 
LVL 1

Author Comment

by:shahnazali
ID: 7010281

hi richie....
       thanks for ur help. It's working fine. But i want it to be called after each and every 15 minutes......... here it is calling only once.....  Can u make it for me....

thanks.

bye,
Shaan....

 
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7011154
This is a little bit complicated.

'In a module

Private Declare Function SetTimer Lib "user32" _
      (ByVal hwnd As Long, _
      ByVal nIDEvent As Long, _
      ByVal uElapse As Long, _
      ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _
      (ByVal hwnd As Long, _
      ByVal nIDEvent As Long) As Long

Public TimerId As Long

Public Sub MyMacro()
    Debug.Print Time
End Sub



Public Function StartTmr(interval As Long)
TimerId = SetTimer(0, 0, interval, AddressOf TimerProc)
End Function


Public Function StopTmr()
KillTimer 0, TimerId
End Function

Sub TimerProc(ByVal hwnd As Long, _
               ByVal uMsg As Long, _
               ByVal idEvent As Long, _
               ByVal dwTime As Long)

    Call MyMacro
End Sub



0
 
LVL 1

Author Comment

by:shahnazali
ID: 7012958
Hi,
 I made little bit modification in ur program according to my requirement, and it's working..... Let me know, Which one is better..... Urs second one or my modified one !!...

Public Sub TimerEvent()
MsgBox "put the action you want to satrt here"
Call_Again
End Sub

Public Sub Call_Again()
Application.OnTime Now()+TimeValue("00:00:5"),"TimerEvent"
End Sub

Private Sub Document_Open()
TimerEvent
End Sub



thanks,

Shaan
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7013678
to me, it is dangerous to call a function inside itself but it has less code than second one.
One problem could be that you couldn't stop the event to fire with your modification.
Hope it helps
0
 
LVL 1

Author Comment

by:shahnazali
ID: 7015703

no richie... that also can be handled easily with some sort of flags....... but i dont need them in my case.......

and why it is dangerous calling one function within another....?? U worry about recursion... in our case.... it's very light.......... right.... It's just one level down... and  i am not sure whether it is recursion or not....... as it's based on thread i feel..... let me know u view...

bye,
Shaan
           
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 7016213
Sorry, i am not against recursion!
Dangerous is a way to speech. I meant, if we don't understand recursion, we easily could fall in an infinite loop, just that.
Cheers
0
 
LVL 1

Author Comment

by:shahnazali
ID: 7017954

Richie deserve the points, as he shows me the right function to call. Using his help, i got my goal...

thanks richie

bye,
Shaan
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7018459
Then, why did you punish me with "B" grade?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
Cannot enter table caption text in Word protected document 6 125
VB 6.0 printer how to align 6 62
Add a task in Outlook from access 11 39
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question