1

Simple Question, I must just be missing something obvious.

I am trying to create a subroutine that polls for a window to be open and I am aiming to accomplish this by a loop that will run while two conditions are met. I keep getting an error:

Run time error 13: Type Mismatch

On the Do while loop, and after searching through the similar issues on SO I still am not quite sure what I am doing wrong.

Here is the line that keeps erroring out:

Sub FindHWND()
    Dim HWNDOut as string
    Dim Timer as Date
    Timer = Now()
    'This following line is the one erroring out. 
    Do While ((Now() < Timer + TimeValue("00:00:10")) And (HWNDOut = 0))
        HWNDOut = CStr(Hex(FindWindowEx(0&, 0, "SunAwtFrame", "Graph.graphml - yEd")))
        Debug.Print HWNDOut
        Sleep (100)
    Loop
    'Other, following sub and code that is not relevant
End Sub

Where Timer is the Now() at moment before the loop starts, and HWNDOut is the handle for the window I am looking for, which will be found in the loop.

All this loop does is look every 100 MS to see if the window to a third party program has opened, in order to prevent the loss of commands in the next subroutines.

Post Script: If anyone has any suggestions how to do this better, I'm all ears. This is my first time using UI Automation so I'm still learning. Edit: Added more code to the block for context.

Community
  • 1
  • 1
JustinCoplin
  • 169
  • 3
  • 16
  • Also, by `'Other, following sub and code that is not relevant` I mean that I commented out everything that didn't deal with the part that I was trying to test. Only the stuff that deals directly with this loop is currently active. – JustinCoplin Dec 08 '17 at 20:36
  • Look in help on the difference between `FindWindow` and `FindWindowEx`. You are using the wrong one. – ACatInLove Dec 08 '17 at 20:58
  • @ACatInLove Just so I can make sure I have it straight, FindWindow Searches for a window with the Class and Window name provided, starting at the root and searching its children one layer at a time. FindWindowEx does mostly the same thing, but it starts at the parent window provided and allows you to exclude some windows by determining what child you begin the search after. Is that about right? – JustinCoplin Dec 11 '17 at 11:14
  • No, Find Windows searches top level windows, eg Notepad's main window. It is also imprecise, it will give you a best match. If two or more windows match it stops looking after the first one. – ACatInLove Dec 12 '17 at 00:20
  • Also the top level (ie App windows) window is a child of the desktop window (hwnd = 0). The desktop window was last seen on Windows 3.11 or if you terminate explorer. It is not YOUR desktop with icons. – ACatInLove Dec 12 '17 at 00:28
  • The Windows' Desktop window shows running programs, as it did in Win 3.1. However to not confuse you they are hidden off screen. https://blogs.msdn.microsoft.com/oldnewthing/20041028-00/?p=37453 – ACatInLove Dec 12 '17 at 19:34

2 Answers2

3

With:

And (HWNDOut = 0))

you are comparing a String to a numeric value..............this will fail.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • Thanks dude, that was totally it, I completely forgot that I used CStr on the Hex value and had HWNDOut as a String. I have left all the other ones as longs in the program and make them strings later. I cant believe I missed that. – JustinCoplin Dec 08 '17 at 20:39
  • You declare HWNDOut and test it in the loop before it is assigned. You will want to assign it a value before the Do While loop starts, which I suppose you intend to be zero initially. Or else the first comparison occurs to a variable that holds nothing. - WWC – Wookies-Will-Code Dec 08 '17 at 20:55
  • 1
    Worth noting, that if `HWNDOut` is indeed `"0"` (or any numeric string), that code *doesn't* throw an error - the right-hand side of the comparison operation is implicitly converted to a string, – Mathieu Guindon Dec 08 '17 at 22:21
0

Try changing variable name Timer to something else e.g. StartTime (think there is a Timer function in VBA which returns a value of a different type; so best not to use words which are reserved or semantically significant).

Might help, might not, good luck.

chillin
  • 4,391
  • 1
  • 8
  • 8
  • "*Might help, might not, good luck.*" - Isn't really a definitive answer, before posting an answer please be sure you're 100% sure – Maldred Dec 08 '17 at 21:08
  • `VBA.DateTime.Timer` is indeed being *shadowed* by this `Timer` local variable. This means an unqualified `Timer` identifier in the scope of that procedure, refers to the local variable and `VBA.DateTime.Timer` would have to be qualified in order for a member expression to resolve correctly - but that is completely unrelated to OP's *type mismatch* error. – Mathieu Guindon Dec 08 '17 at 22:23