Looping a find window

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I really haven't mastered the concept of doing a loop in VBA (seriously need to find a good resource to practice with), however, I have a code that will find a window and pull it to the foreground, but if the window doesn't exist, I want to wait for five seconds and check again, now as I safety measure, I should perhaps put a two minute limit to prompt the user to retry for another two minutes or kill the code. Any help if getting the loop setup would be appreciated.

VBA Code:
Sub ShowIt2()
    Dim hWnd As Long
   
    hWnd = FindWindow(vbNullString, "Save As")
    If hWnd > 0 Then SetForegroundWindow hWnd
    Else
    Application.Wait (Now + TimeValue("0:0:05"))
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Maybe something like this?

VBA Code:
Sub ShowIt2()

    Const MAX_WAIT_SECS As Long = 120
    Const WAIT_SECS As Long = 5

    Dim hWnd As Long
    Dim ans As VbMsgBoxResult
    Dim endTime As Single
  
    endTime = Timer + MAX_WAIT_SECS
  
    Do
        hWnd = FindWindow(vbNullString, "Save As")
        If hWnd > 0 Then
            SetForegroundWindow hWnd
            Exit Do
        End If
        If Timer > endTime Then
            ans = MsgBox("Window not found, try again?", vbQuestion + vbYesNo)
            If ans = vbYes Then
                endTime = Timer + MAX_WAIT_SECS
            Else
                Exit Do
            End If
        Else
            PauseMacro WAIT_SECS
        End If
    Loop

End Sub

Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
  
    Do
        DoEvents
    Loop Until Timer > endTime
  
End Sub
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top