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/index.do,https://servman/sm/index.do#,https://servman/sm/index.do?lang=en,https://servman/sm/index.do?lang=en&mode=index.do,https://servman/sm/index.do?lang=en&mode=index.do&logout_msg=LogoutPage.session_timeout"
  
    ' 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
                Next
                If blnFoundWebSite = True Then Exit For
            End If
        End If
    Next
    
    
    If blnFoundWebSite Then
        blnRefreshFound = False
        
        For Each btn In objIEDoc.getElementsByTagName("button")
            If btn.qtip = "Refresh" And btn.ID <> "ext-gen28" Then
                btn.Click
                blnRefreshFound = True
                Exit For
            End If
        Next btn
    
        If Not blnRefreshFound Then
            MsgBox ("No refresh button found")
        End If
    Else
        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

kbay808Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbay808Author Commented:
That’s awesome!!!  Thank you very much.
0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.