Looping FindWindow inside a userform

anichols

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

I have a managed to find and modify some VBA that will locate as "Save As" window and then paste a file location inside it. My challenge, is that once time expires, a message box pops up prompting a stop or retry. What I am trying to do is instead of a message box, change a few things on my userform, then exit the loop, this would then allow the user to choose to retry or go to a different task via various command buttons. My code to exit this part is flawed, and seems to run without end.

Here is the function code with the message box:
VBA Code:
Sub ShowMeTheFile1x()

    Const MAX_WAIT_SECS As Long = 25
    Const WAIT_SECS As Long = 5

    Dim hWnd As Long
    Dim ans As VbMsgBoxResult
    Dim endTime As Single
  
    endTime = Timer + MAX_WAIT_SECS
    Sheets("DAILY01").Select
    Range("B20").Select
    Selection.Copy
    Do
        hWnd = FindWindow(vbNullString, "Save As")
        If hWnd > 0 Then
            SetForegroundWindow hWnd
            Application.SendKeys ("^v")
            Application.SendKeys ("~")
            Exit Do
        End If
        If Timer > endTime Then
            Exit Do
            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
Here is the code as I am trying (unsuccessfully) to modify it:
VBA Code:
Sub ShowMeTheFile1x()

    Const MAX_WAIT_SECS As Long = 25
    Const WAIT_SECS As Long = 5

    Dim hWnd As Long
    Dim ans As VbMsgBoxResult
    Dim endTime As Single
  
    endTime = Timer + MAX_WAIT_SECS
    Sheets("DAILY01").Select
    Range("B20").Select
    Selection.Copy
    Do
        hWnd = FindWindow(vbNullString, "Save As")
        If hWnd > 0 Then
            SetForegroundWindow hWnd
            Application.SendKeys ("^v")
            Application.SendKeys ("~")
            Exit Do
        End If
        If Timer > endTime Then
            Exit Do
            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
Any assistance would be greatly appreciated
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,948
Messages
6,175,579
Members
452,653
Latest member
craigje92

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