VBA error handling problem

robw

Board Regular
Joined
Dec 18, 2002
Messages
159
Hi,
I’m having a problem with the following tract of VBA. It is intended that it should loop round until a window with a particular title (as held in string wtit) is detected. It works ok if the said window already exists before running the code, but it fails on the SECOND time round the loop if the window does not pre-exist.

I assume I’m doing something wrong in the error handling or error clearing but I cannot figure it out. Any suggestions on what I’m doing wrong, please?

Many Thanks
Rob

Code:
    For appacttry = 1 To 3
        On Error GoTo appactfail
            AppActivate wtit  ‘fails HERE on the second time round the loop  
                                              ‘with message “Run-time error 5 – Invalid procedure call or argument
        On Error GoTo 0
        GoTo appactok
appactfail:
        Err.clear  
        On Error GoTo 0
        MsgBox "failed to find window on try number  " & appacttry  
  Next appacttry
appactok:
 
Last edited by a moderator:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
33,745
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,085,589
Messages
5,384,624
Members
401,913
Latest member
chethan av

Some videos you may like

This Week's Hot Topics

Top