Solved

Timer for macro...

Posted on 2002-05-14
10
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

739 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