Table of contents

QueryClose 事件QueryClose Event

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

用户窗体关闭之前发生。语法专用子 UserForm_QueryClose (取消为整数closemode为整数)QueryClose事件语法具有下列组成部分:Occurs before a UserForm closes. SyntaxPrivate Sub UserForm_QueryClose(cancelAs Integer, closemodeAs Integer) The QueryClose event syntax has these parts:

cancelcancel一个整数。设置此参数为非 0 停止任何值 QueryClose 事件中所有加载用户窗体,可以防止用户窗体和应用程序不能关闭。An integer. Setting this argument to any value other than 0 stops the QueryClose event in all loaded user forms and prevents the UserForm and application from closing.
closemodeclosemode值或常量,指示 QueryClose 事件的原因。A value or constant indicating the cause of the QueryClose event.

返回值Closemode_参数会返回下列值:Return Values The _closemode argument returns the following values:

vbFormControlMenuvbFormControlMenu00用户已从用户窗体上的控件菜单中选择关闭命令。The user has chosen the Close command from the Control menu on the UserForm.
vbFormCodevbFormCode11Unload语句将从代码中调用。The Unload statement is invoked from code.
vbAppWindowsvbAppWindows22当前 Windows 操作环境会话正在关闭。The current Windows operating environment session is ending.
vbAppTaskManagervbAppTaskManager33Windows任务管理器关闭该应用程序。The Windows Task Manager is closing the application.

Visual Basic for Applications对象浏览器中的对象库中列出了这些常量。请注意, vbFormMDIForm还指定在对象浏览器器中,但尚不支持。备注此事件通常用于确保该应用程序关闭之前,应用程序中包含的用户窗体中没有未完成的任务。例如,如果用户没有保存在任何用户窗体中的新数据,应用程序可以提示用户保存数据。当应用程序关闭时,您可以使用QueryClose事件过程将Cancel属性设置为True,则停止关闭进程。These constants are listed in the Visual Basic for Applications object library in theObject Browser. Note that vbFormMDIForm is also specified in the Object Browser, but is not yet supported. Remarks This event is typically used to make sure there are no unfinished tasks in the user forms included in an application before that application closes. For example, if a user hasn't saved new data in any UserForm, the application can prompt the user to save the data. When an application closes, you can use the QueryClose event procedure to set the Cancel property to True, stopping the closing process.


下面的代码将强制用户单击以将其关闭的用户窗体的工作区。如果用户尝试使用标题栏中的关闭框,取消_参数设置为非零值,防止终止。但是,如果用户已单击工作区, _CloseMode_具有值 1 和Unload Me执行。The following code forces the user to click the UserForm's client area to close it. If the user tries to use the Close box in the title bar, the _Cancel parameter is set to a nonzero value, preventing termination. However, if the user has clicked the client area, CloseMode has the value 1 and Unload Me is executed.

Private Sub UserForm_Activate()
    UserForm1.Caption = "You must Click me to kill me!"
End Sub

Private Sub UserForm_Click()
  Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    'Prevent user from closing with the Close box in the title bar.
    If CloseMode <> 1 Then Cancel = 1
    UserForm1.Caption = "The Close box won't work! Click me!"
End Sub
© 2018 Microsoft