Solved

Timer for macro...

Posted on 2002-05-14
10
271 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 64
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 54
Formula problem with Excel attachment 6 39
checkbox to hide entire section 10 42
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

839 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