Take the 2-minute tour ×

I am writing a scrip in VBA which will monitor the registry value and if it changes the script will overwrite it. Till now I wrote an script which checks the value in the registry and updates it to given value. But my problem is how to update the registry each 2 minutes or when the value changes ? Please help !!!

On Error Resume Next
strComputer = "."
strKeyPath = "Control Panel\Desktop"
ValueName = "ScreenSaveActive"
    objReg.GetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue
    objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
If IsNull(strValue) Then
    Wscript.Echo "The value is either Null or could not be found in the registry."
    Wscript.Echo "Value: ", strValue
    strValue = "1"
    objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue
    Wscript.Echo "Value after change ", strValue
End If

Regards, Erni

share|improve this question
did you mean VBA or VbScript? Now you have two answers, one for each script language. :-) –  Matt Jul 18 '12 at 16:36
Imagine if two programs did this. –  Raymond Chen Jul 19 '12 at 13:13
add comment

2 Answers

up vote 2 down vote accepted

Just put your code inside a loop and tell it to sleep for 2 minutes between each time through the loop, so something like this would probably work:

Do While 1

    [your code here]

    WScript.Sleep(120 * 1000) ' 120 seconds in 2 minutes and it expects milliseconds

But if the value is null, then there probably isn't any point in carrying on, so it might be worth adding a Exit Do line after the Echo about it being null to stop the loop if that's the case.

Please note though, you're using VBscript, not VBA (they are similar, but still different languages).

share|improve this answer
But with this approach you cannot do anything but the loop. Look at my example - there the timer code runs asynchronously in the background and you can do other things in parallel. –  Matt Jul 18 '12 at 16:24
@Matt: True, but as I said in my answer, I think he had mistaken VBA for VBScript, since his code included Wscript.Echo and similar. Looking at the code I also think that this is probably some kind of quick and dirty hack solution, so making it "proper" is probably not a priority (otherwise I'd suggest a C++ Windows Service would be a good option). –  ho1 Jul 18 '12 at 18:28
Indeed, I would argue that code that overwrites a registry setting every two seconds is by definition a quick and dirty hack. :-) –  Harry Johnston Jul 18 '12 at 20:18
ho1, Harry: I agree to what you've said. Ernie: Maybe its worth trying it in VisualStudio or Sharpdevelop (free) - with vb.net or c# there is much more possible. –  Matt Jul 19 '12 at 6:58
add comment

The following example is done in Excel VBA. Put the following code in ThisWorkbook:

Option Explicit
Public TimerActive As Boolean
Public TimerInterval As String ' e.g. "00:02:00"
Public TimerProcedure As String
Private NextSchedule As Variant

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    TimerActive = False
    Application.OnTime EarliestTime:=NextSchedule, _
                       Procedure:=TimerProcedure, Schedule:=False
End Sub

Private Sub Workbook_Open()
    TimerProcedure = "Module1.myTimerProc"
    TimerInterval = "00:00:10"
    TimerActive = True
    ScheduleTimer TimerInterval, TimerProcedure
End Sub

Public Sub ScheduleTimer(nextInterval As String, procName As String)
    NextSchedule = Now + TimeValue(nextInterval)
    Application.OnTime NextSchedule, procName
End Sub

Then, create Module1 as with the following procedure in it:

Option Explicit
Public Sub myTimerProc()
    With ThisWorkbook
        If .TimerActive Then
            Debug.Print Now & " Hi!"
            .ScheduleTimer .TimerInterval, .TimerProcedure
        End If
    End With
End Sub

This prints in the debug (immediate) window every 10 seconds the message "Hi!". You can easily modify the example to write into the registry - just create another procedure in this module and replace the Debug.Print statement by a call of the procedure you've written. The timer is activated right after you've opened the excel file and deactivated before you close it.

Note that

  • you need the TimerActive flag in the Workbook_Close event along with the if statement in myTimerProc to allow Excel to properly close: If you don't have this extra code it will re-fire the timer indefinitely.
  • the file needs to be saved as "macro enabled workbook" and probably needs to be signed or put into a trusted location. Otherwise, the macros in it will not run (disabled by Excel).
share|improve this answer
add comment

Your Answer


