Solved

Timer for macro...

Posted on 2002-05-14
10
268 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help in WHSCRIPT 9 41
passing parameter in sql procedure 9 50
Microsoft Access combo box help 2 30
Copy a row 12 53
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now