Casting the front window - found workaround

The code below set w = Nil at IDE (and application) run time:

If Window(0) IsA wMain Then w = wMain(Window(0)) End If

At the time the code above is executed, the application front window IsA wMain.

But w is Nil.

I know it is not a reference, but that code works fine when running both Windows (XP - 8.1 - both 10).

Idea ?

PS: the code is in a MenuItem. Other windows can appears frontmost thus the test.

I found a workaround.

I set the code above in the MenuHandler and store the result in a Module Property. Works fine now.