A
Anne P.
I have a template named "SK Interoffice Memorandum.dot." The userform has a
command button named Cancel which contains the following code:
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
After clicking the Cancel button, if No is clicked on the message box, the
focus is returned to the userform, but if Yes is clicked, the form is
unloaded and the document is closed without saving changes.
The client now wants the same actions to occur when the user clicks the X
button on the userform. I added the following code to the form to effect
this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = False
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
End Sub
After adding the UserForm_QueryClose event as above, I get the following
results: After clicking the X button, if No is clicked on the message box,
it unloads the userform and leaves the document open in Word, when it should
return focus to the userform. If the user clicks Yes, it unloads the
userform and closes the document; however, it leaves a temporary file in the
same directory where the original template is stored (named ~$ Interoffice
Memo.dot). Also, the behavior of the Cancel button has now changed to the
following: If No is clicked, the focus is returned to the userform as
expected. If Yes is clicked, nothing happens, then if Yes is clicked a
second time, the userform is unloaded and the document is removed from the
screen. However, it appears that Word is trying to remove the document a
second time because I get an error message that says: "Command is not
available because no document is open".
I then changed the QueryClose event as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = False
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
Else
Cancel = True
End If
End Sub
I now get the following results: Clicking the X button works the same as
before when clicking either No or Yes. The behavior of the Cancel button
has now changed to the following: If No is clicked, the focus is returned
to the userform as expected. If Yes is clicked, the document is removed
from the screen but the userform is still there. Then the only way to close
the userform is to click the X button and choose Yes, at which point I get a
debug error 4148 stating "Command is not available because no document is
open".
Any insights on how I can make this work whether the X button or Cancel
button is clicked? Thanks,
Anne P.
command button named Cancel which contains the following code:
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
After clicking the Cancel button, if No is clicked on the message box, the
focus is returned to the userform, but if Yes is clicked, the form is
unloaded and the document is closed without saving changes.
The client now wants the same actions to occur when the user clicks the X
button on the userform. I added the following code to the form to effect
this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = False
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
End Sub
After adding the UserForm_QueryClose event as above, I get the following
results: After clicking the X button, if No is clicked on the message box,
it unloads the userform and leaves the document open in Word, when it should
return focus to the userform. If the user clicks Yes, it unloads the
userform and closes the document; however, it leaves a temporary file in the
same directory where the original template is stored (named ~$ Interoffice
Memo.dot). Also, the behavior of the Cancel button has now changed to the
following: If No is clicked, the focus is returned to the userform as
expected. If Yes is clicked, nothing happens, then if Yes is clicked a
second time, the userform is unloaded and the document is removed from the
screen. However, it appears that Word is trying to remove the document a
second time because I get an error message that says: "Command is not
available because no document is open".
I then changed the QueryClose event as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = False
If MsgBox("Are you sure?", vbYesNo + vbInformation, _
"Seward & Kissel Interoffice Memo") = vbYes Then
Unload Me
ActiveDocument.Close (wdDoNotSaveChanges)
End If
Else
Cancel = True
End If
End Sub
I now get the following results: Clicking the X button works the same as
before when clicking either No or Yes. The behavior of the Cancel button
has now changed to the following: If No is clicked, the focus is returned
to the userform as expected. If Yes is clicked, the document is removed
from the screen but the userform is still there. Then the only way to close
the userform is to click the X button and choose Yes, at which point I get a
debug error 4148 stating "Command is not available because no document is
open".
Any insights on how I can make this work whether the X button or Cancel
button is clicked? Thanks,
Anne P.