Glad you found the solution. Yes I've had a similar issue and added comment notes at the time in one of my scripts :
GUIDelete($hGUI_Preview) ; delete the window (the handle $hGUI_Preview still contains a pointer, these are 2 different points)
$hGUI_Preview = 0 ; force the variable (handle) $hGUI_Preview to 0, then it's not a pointer any more but an Int32 variable.
; It's the same principle with plenty of other functions, for example _GDIPlus_ImageDispose($hImage)
; which frees memory, but their handle still contains its value AFTER _GDIPlus_ImageDispose($hImage)
; When needed, it's the programmer's job to empty the variable ($hImage = 0 or $hGUI_Preview = 0) making sure
; the variable is no more a handle (pointer) but an Int32 variable, as VarGetType() would reflect it.
; Only then, a test such as If $hGUI_Preview Then... will make sense, no matter its place in the script.
Edit: I got additional comments placed at the beginning of the script. Gonna add them below in case it may help a user facing the same problem :
Global $hGUI_Preview = 0
#cs
Note: it's a good thing to initialize all Global handles to 0 : it would avoid (for instance) an error during tests like "If WinExists($hGUI_Preview) Then..." on an untitled undefined window. Don't forget that a variable defined with "Global $hGUI_Preview" creates a STRING variable and then a test like If WinExists("") would return 1 (success) which is a bad thing in this case, while If WinExists(0) would return 0 (failure) and this is what we need. That explains why $hGUI_Preview = 0 has to be placed immediately after GUIDelete($hGUI_Preview) in the script, it's done !
It's VERY important to force $hGUI_Preview = 0 just after GUIDelete($hGUI_Preview) as we intend to use tests like "If WinExists($hGUI_Preview) Then..." or "If $hGUI_Preview Then..." anywhere in the script.
#ce