How to create a macro that will run another macro every 5 seconds that can be turned on and off?

The below code refreshes an iFrame that is within a website.  I would like to create a macro that will run the code every 5 seconds and another to turn it off.

Sub ClickRefresh()

    Dim objIE As SHDocVw.InternetExplorer
    Dim blnFoundWebSite As Boolean
    Dim strURL As Variant
    Set objShell = New SHDocVw.ShellWindows

' **** Set the Web Address Here ****
    strURLS = "https://servman/sm/,https://servman/sm/,https://servman/sm/,https://servman/sm/,https://servman/sm/"
    ' Look for the appropriate open IE Window
    For Each objIE In objShell
        If TypeOf objIE.document Is MSHTML.HTMLDocument Then
            Set objIEDoc = objIE.document
            If Not (objIEDoc Is Nothing) Then
                For Each strURL In Split(strURLS, ",")
                    If objIEDoc.url = strURL Then
                        blnFoundWebSite = True
                        Exit For
                    End If
                If blnFoundWebSite = True Then Exit For
            End If
        End If
    If blnFoundWebSite Then
        blnRefreshFound = False
        For Each btn In objIEDoc.getElementsByTagName("button")
            If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then
                blnRefreshFound = True
                Exit For
            End If
        Next btn
        If Not blnRefreshFound Then
            MsgBox ("No refresh button found")
        End If
        MsgBox strURL & " was not found" & vbCrLf & "in an open Internet Explorer Window", vbExclamation, "No Page Found"
    End If
    Set objIE = Nothing
    Set objShell = Nothing
    Set objIEDoc = Nothing
End Sub

Open in new window

Who is Participating?
Martin LissOlder than dirtCommented:
Option Explicit

Public Declare Function SetTimer Lib "user32" ( _
    ByVal HWnd As Long, _
    ByVal nIDEvent As Long, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As Long) As Long

Public Declare Function KillTimer Lib "user32" ( _
    ByVal HWnd As Long, _
    ByVal nIDEvent As Long) As Long

Public TimerID As Long
Public TimerSeconds As Single

Sub StartTimer()
    TimerSeconds = 5 ' how often to "pop" the timer in seconds.
    TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf ClickRefresh)
End Sub

Sub EndTimer()
    On Error Resume Next
    KillTimer 0&, TimerID
End Sub

Open in new window

kbay808Author Commented:
That’s awesome!!!  Thank you very much.
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.