excel - 禁用 VBA 用户窗体 'x' ,但仍允许卸载我

标签 excel vba userform

我有一个用户表单,在关闭时需要运行清理步骤。我想要 X按钮被禁用和/或不可见,但我仍然需要能够卸载表单。我使用了如下代码,但它也阻止了 Unload Me .

'Disables closing via x button
Sub UserForm_QueryClose(Cancel As Integer, ClsoeMode As Integer)
    If CloseMode = vbFormControlMenu Then
        MsgBox ("BLOCKED")
        Cancel = True
    End If
End Sub

最佳答案

不要使用 UserForm_QueryClose在这种情况下。使用 API RemoveMenu , GetSystemMenuFindWindow
This是我最喜欢的 API 网站

删除菜单 : http://allapi.mentalis.org/apilist/RemoveMenu.shtml

获取系统菜单 : http://allapi.mentalis.org/apilist/GetSystemMenu.shtml

查找窗口 : http://allapi.mentalis.org/apilist/FindWindow.shtml

看这个例子

Option Explicit

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Const MF_BYPOSITION = &H400&

Private Sub UserForm_Initialize()
    Dim Ret As Long

    '~~> Change UserForm1 to match your userform's caption
    Ret = FindWindow("ThunderDFrame", "UserForm1")

    Do While Ret = 0
        '~~> Change UserForm1 to match your userform's caption
        Ret = FindWindow("ThunderDFrame", "UserForm1")
        DoEvents
    Loop

    RemoveMenu GetSystemMenu(Ret, 0), 6, MF_BYPOSITION
End Sub

Private Sub CommandButton1_Click()
    Unload Me
End Sub

截图 :

enter image description here

关于excel - 禁用 VBA 用户窗体 'x' ,但仍允许卸载我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15808997/

相关文章:

regex - 使用正则表达式替换 - VBA

vba - 如何选择/激活多个工作表以将用户表单数据传输到单元格?

excel - 在powershell中打开受密码保护的Excel

arrays - 在动态多维数组中搜索、排序、匹配 - VBA

javascript - 具有多个选项和不同权重的排列

c++ - VBA 中的 Qt5 Dll 出现错误运行时错误 453

excel - 导出适合一页的 PDF

excel - VBA UserForm 为其参数之一提供运行时错误 91

vba - 行到字符串,项目之间有空格

java - 下载文件 java spring rest api