PDA

View Full Version : Solved: QueryUnload is firing in an unused Form



Howard Kaikow
01-09-2005, 03:32 AM
I have a VB 6 ActiveX DLL that contains 12 Forms.

The DLL is used by Word.

A toolbar/menu starts macros that invoke the 6 main Forms.

Each Form has a QueryUnload event similar to the one given below.
Only 1 of the Forms has the specific Msgbox listed below. The others have
quite different text.

However, yesterday, something very odd started happening.
I would start a macro, and use the appropriate buttons to close the Form.
No problem doing this.

However, when I then exit from Word, somehow, this causes the code below to
be invoked a Form that was not even used during the current Word session.
The message below is issued, even tho the Form was not used and the Close
box was not used with any Form.
After I respond to the Msgbox, less than a minute later, I get a nasty
screen stating that Word must close and asking whether I wish to send a
report to Microsoft.

The same thing happens no matter which Form is used. As far as I know the
Form that contains the particular QueryLoad event is NOT being loaded.

What could cause this behavior?


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Prevent user from closing with the Close box in the title bar.
If UnloadMode = vbFormControlMenu Then
Cancel = 1
MsgBox "Please use the Exit tab to close the Form", _
vbInformation + vbOKOnly, "Close button cannot be used to close
the Form"
Else
Cancel = 0
End If
End Sub

Howard Kaikow
01-09-2005, 12:10 PM
Found the problem.

On 5 January, I added the OPENFILENAME structure to the sub that
initializes the class.

In that structure, I used

.hwndOwner = frmX.hWnd

So the form was indeed being loaded every time.

However, since adding the structure, until yesterday, I had only been
testing with frmX, not the other forms, so I did not see the problem.

The solution appears to be to not set .hwndOwner, so I just removed the line

.hwndOwner = frmX.hWnd

I guess that UnloadMode gets set to vbFormControlMenu when such a form gets
unloaded, even tho the Close box was not used.

Howard Kaikow
02-01-2005, 02:16 PM
Yes, I violated my religion by marking this thread as Solved, but it felt oh so good!!