PDA

View Full Version : Problem on closing userform with API handles



Nuz
12-02-2011, 01:14 AM
I have created a userform that utilizes API calls to create a menu and to display an icon on the header.

When the userform is closed, the Excel freezes and becomes unresponsive. I figured it out it is due to the menu created with API's.

I have some cleaning measures with UserForm_QueryClose and UserForm_Terminate procedures but they seem not to help.
In both procedures I have:
DestroyMenu g_hMenu
SetWindowLong g_hForm, GWL_WNDPROC, g_lpMyWndProc
and:
Public Declare Function DestroyMenu _
Lib "user32" ( _
ByVal hMenu As Long) _
As Long

Public Declare Function SetWindowLong _
Lib "user32" _
Alias "SetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) _
As Long
How can I prevent Excel from freezing when the userform is closed?

Aflatoon
12-02-2011, 02:10 AM
Probably need to see the rest of the code. I am not currently sure what the SetWindowLong part is for when the window is being destroyed anyway.

JP2112
12-02-2011, 12:23 PM
Is there any reason why this can't be done wholly in VBA (i.e. without using API calls)?

Nuz
12-04-2011, 11:30 PM
API's are used to create a menu on the userform and to display an icon on the userform header. I don't know any other way to do it (unfortunately).

Currently, when the userform is closed I have to force the Excel to close also to overcome this problem. This is an awful way to do it since if the user has any other workbooks open they are closed as well.