Creating a Timer/Loop inside a Macro

Hi, I'm using MS Word 2003. I would like to create a Loop within one my macros that executes or Loops on a timed basis. In other words, once I trigger my macro, I'd like something within the macro to happen every 15 minutes.

For example, lets say I wanted to print my document every 15 minutes. So, I have my print command within my macro, which is simple to set up. Now I want to put that print event inside a Loop that fires every 15 minutes. In the end I'd be printing my document every 15 minutes as long as my macro is running.

What would be the best way to do that?

Thanks,
Fulano
Mr_FulanoAsked:
Who is Participating?
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Connect With a Mentor Microsoft MVP ExcelCommented:
Hello,

this code goes into several modules, to automatically kick in when the workbook is opened.

cheers, teylyn
' Code for the ThisWorkbook module
Private Sub Workbook_BeforeClose(Cancel As Boolean)

     Application.OnTime dTime, "MyTimer", , False

End Sub



Private Sub Workbook_Open()

  Application.OnTime Now + TimeValue("00:15:00"), "MyTimer"

End Sub



' this code goes in a standard module

Public dTime As Date
Sub MyTimer()
dTime = Now + TimeValue("00:15:00")
Application.OnTime dTime, "MyTimer"


'put your code here
End Sub

Open in new window

0
 
Mr_FulanoAuthor Commented:
Hi teylyn, OK...that worked. How do I stop it if I want to stop it without closing the document?

Thanks for the help!
Fulano
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
You need to run another macro to stop the timer. You could add a form button or a shape to the sheet and assign the macro below to that button.

cheers, teylyn


Sub StopMyTimer()
    Application.OnTime dTime, "MyTimer", , False 
End Sub

Open in new window

0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Mr_FulanoAuthor Commented:
Very good...thank you!!!

Fulano
0
 
Mr_FulanoAuthor Commented:
Excellent work!
0
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Thanks for the grade!
0
All Courses

From novice to tech pro — start learning today.