在我的进度条上隐藏excel vba用户窗体上的closuresbutton

我创build了一个用户窗体,当macros仍在导入工作表时,显示一个进度条 在这里输入图像说明

问题是用户可以按红色[X]buttonclosures并中断处理完成。

有没有办法隐藏厄运的红色button,以便潜在用户在运行时不会有任何令人困惑的button点击。

编辑:

我已经试过了

'Find the userform's Window Private Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long 'Get the current window style Private Declare Function GetWindowLong Lib "user32" _ Alias "GetWindowLongA" ( _ ByVal hWnd As Long, _ ByVal nIndex As Long) As Long 'Set the new window style Private Declare Function SetWindowLong Lib "user32" _ Alias "SetWindowLongA" ( _ ByVal hWnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long) As Long Const GWL_STYLE = -16 Const WS_SYSMENU = &H80000 

我在userform_initialize上使用了这个

  Dim hWnd As Long, lStyle As Long 'Which type of userform If Val(Application.Version) >= 9 Then hWnd = FindWindow("ThunderDFrame", Me.Caption) Else hWnd = FindWindow("ThunderXFrame", Me.Caption) End If 'Get the current window style and turn off the Close button lStyle = GetWindowLong(hWnd, GWL_STYLE) SetWindowLong hWnd, GWL_STYLE, (lStyle And Not WS_SYSMENU) 

我收到此错误消息 在这里输入图像说明

这个代码是从这里拿走的。 我不知道我做错了什么,我已经删除了评论。 这是我find的最简单的代码,所以我想将它集成到我的用户表单中。 任何帮助表示赞赏。

下面是你可以这样调用的例程:

 subRemoveCloseButton MyForm 

或从您的表单中:

 subRemoveCloseButton Me 

以下是您需要的代码:

 Private Const mcGWL_STYLE = (-16) Private Const mcWS_SYSMENU = &H80000 'Windows API calls to handle windows #If VBA7 Then Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long #Else Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long #End If #If VBA7 Then Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long #Else Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long #End If #If VBA7 Then Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long #Else Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long #End If Public Sub subRemoveCloseButton(frm As Object) Dim lngStyle As Long Dim lngHWnd As Long lngHWnd = FindWindow(vbNullString, frm.Caption) lngStyle = GetWindowLong(lngHWnd, mcGWL_STYLE) If lngStyle And mcWS_SYSMENU > 0 Then SetWindowLong lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU) End If End Sub 

你可以从下面的代码片断中find它:

selectcmdClosebutton在菜单栏上,selectView | Code View | Code在光标闪烁的地方,input以下代码:

 Private Sub cmdClose_Click() Unload Me End Sub 

在菜单栏上,selectView | Object View | Object ,返回到UserForm。

要允许用户通过按Esc键closures窗体:

selectcmdClosebutton在“属性”窗口中,将“ Cancel属性更改为“ True

通过单击Xbutton来防止用户closures表单

UserForm打开时,右上angular有一个X 除了使用“closures窗体”button之外,人们还可以使用Xclosures窗体。如果要防止这种情况,请按照下列步骤操作。

用鼠标右键单击用户窗体的空白部分selectView | Code View | Code从“过程”下拉列表中,在右上angularselect“查询closures”

如果光标闪烁,请粘贴以下示例中突出显示的代码

 Private Sub UserForm_QueryClose(Cancel As Integer, _ CloseMode As Integer) If CloseMode = vbFormControlMenu Then Cancel = True MsgBox "Please use the Close Form button!" End If End Sub 

在菜单栏上,selectView | Object View | Object ,返回到UserForm。 现在,如果有人点击UserForm中的X ,他们会看到你的消息。

http://www.contextures.com/xlUserForm01.html

我发现贾斯汀·戴维斯的贡献相当有帮助:如果你更换线

 MsgBox "Please use the Close Form button!" 

 UserformX.hide 

用户窗体隐藏在用户面前,但在卸载之前仍处于活动状态。

询问用户是否要closures表单 – 并丢失编辑(比如说)。 基于Justin&Peter的想法。

 Private Sub UserForm_QueryClose(Cancel As Integer, _ CloseMode As Integer) Dim ans If CloseMode = vbFormControlMenu Then Cancel = True ans = Msgbox("Cancel edit?", vbQuestion + vbYesNo) If ans = vbYes Then Me.Hide End if End If End Sub 

编辑:其实我意识到这是一个有点偏离主题,因为OP想要删除X选项 – 但我仍然发现这个交互式表单很方便。

这是@彼得·艾伯特的上述回答的改进

  • Windows API调用现在是Office x64安全的
  • FindWindow调用已得到改进,仅查找Excel用户窗体。 原始答案中的functionsearch每个窗口类(例如,资源pipe理器窗口和其他程序的窗口)。 因此,当其名称与UserForm名称相同时,可能会发生其他程序或资源pipe理器窗口的[x]button被删除。

 Private Const mcGWL_STYLE = (-16) Private Const mcWS_SYSMENU = &H80000 'Windows API calls to handle windows Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr #If Win64 Then Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongPtrA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongPtrA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr #Else Private Declare PtrSafe Function GetWindowLongPtr Lib "user32" Alias "GetWindowLongA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long) As LongPtr Private Declare PtrSafe Function SetWindowLongPtr Lib "user32" Alias "SetWindowLongA" ( _ ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr #End If Public Sub RemoveCloseButton(objForm As Object) Dim lngStyle As LongPtr Dim lngHWnd As LongPtr Dim lpClassName As String lpClassName = vbNullString If Val(Application.Version) >= 9 Then lpClassName = "ThunderDFrame" Else lpClassName = "ThunderXFrame" End If lngHWnd = FindWindow(lpClassName, objForm.Caption) lngStyle = GetWindowLongPtr(lngHWnd, mcGWL_STYLE) If lngStyle And mcWS_SYSMENU > 0 Then SetWindowLongPtr lngHWnd, mcGWL_STYLE, (lngStyle And Not mcWS_SYSMENU) End If End Sub 

ThunderDFrame?
Excel中的UserForms实际上是Windows类ThunderDFrame ,它是2002年以后Microsoft Office应用程序中所有UserFroms的类。在此之前,它是ThunderXFrame

我知道这是一个老问题,但是对于引用的OPtypes的用户,您不必删除,隐藏或禁用closuresbutton。 有一个更简单的方法;)

对于任何没有任何用户交互元素(button等)的用户表单,并在完成其目的后自行closures,只需禁用表单即可。

要禁用用户窗体:在用户窗体的属性中,按照Enabled设置False。 用户表单将显示,直到代码告诉它隐藏。 用户不能对表单做任何事情(不能closures,不能移动等)。

还要注意的是,当用户窗体仍在显示时,是否希望用户能够在主窗口中执行其他任何操作,以决定是否设置ShowModal。

一个有用的方法来禁用button是做到以下几点:

 Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then Cancel = True End Sub 

虽然这并没有摆脱button,但它确实使点击它没有任何东西。