Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Timer for macro...

Posted on 2002-05-14
10
Medium Priority
?
277 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 150 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

660 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