Excel 如何更改消息框上按钮的名称?
我想写一个宏,在打开电子表格时执行,并显示一个消息框,其中有三个按钮,上面有自定义文本。当用户单击其中一个按钮时,它将根据按下的按钮执行另一个宏 我已经了解了如何在打开时运行宏,以及如何编写消息框,但不使用自定义文本Excel 如何更改消息框上按钮的名称?,excel,vba,Excel,Vba,我想写一个宏,在打开电子表格时执行,并显示一个消息框,其中有三个按钮,上面有自定义文本。当用户单击其中一个按钮时,它将根据按下的按钮执行另一个宏 我已经了解了如何在打开时运行宏,以及如何编写消息框,但不使用自定义文本 这可能吗?多年前,我从一个论坛(我想是Excel先生)复制了这段代码-您可能需要使用声明来适应您的VBA和Windows 我确实记得在过去使用过这段代码。我很难为情地说,这是一个公然的货运邪教编程案例,我没有努力学习代码中使用的挂钩。如果您希望采用这些程序,Siddharth Ro
这可能吗?多年前,我从一个论坛(我想是Excel先生)复制了这段代码-您可能需要使用声明来适应您的VBA和Windows 我确实记得在过去使用过这段代码。我很难为情地说,这是一个公然的货运邪教编程案例,我没有努力学习代码中使用的挂钩。如果您希望采用这些程序,Siddharth Rout的建议值得注意。无论如何,内疚感占据了我的上风,我转而使用定制的
Userforms
,正如吉佩德所建议的那样
Option Explicit
Private Const MB_YESNOCANCEL = &H3&
Private Const MB_YESNO = &H4&
Private Const MB_RETRYCANCEL = &H5&
Private Const MB_OKCANCEL = &H1&
Private Const MB_OK = &H0&
Private Const MB_ABORTRETRYIGNORE = &H2&
Private Const MB_ICONEXCLAMATION = &H30&
Private Const MB_ICONQUESTION = &H20&
Private Const MB_ICONASTERISK = &H40&
Private Const MB_ICONINFORMATION = MB_ICONASTERISK
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const IDABORT = 3
Private Const IDRETRY = 4
Private Const IDIGNORE = 5
Private Const IDYES = 6
Private Const IDNO = 7
Private Const IDPROMPT = &HFFFF&
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type
Private MSGHOOK As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias _
"MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, _
ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function SetDlgItemText Lib "user32" Alias _
"SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, _
ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias _
"SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Dim mbFlags As VbMsgBoxStyle
Dim mbFlags2 As VbMsgBoxStyle
Dim mTitle As String
Dim mPrompt As String
Dim But1 As String
Dim But2 As String
Dim But3 As String
Public Function MessageBoxH(hwndThreadOwner As Long, _
hwndOwner As Long, mbFlags As VbMsgBoxStyle) As Long
Dim hInstance As Long
Dim hThreadId As Long
hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
With MSGHOOK
.hwndOwner = hwndOwner
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
End With
MessageBoxH = MessageBox(hwndOwner, Space$(120), Space$(120), mbFlags)
End Function
Public Function MsgBoxHookProc(ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = HCBT_ACTIVATE Then
SetWindowText wParam, mTitle
SetDlgItemText wParam, IDPROMPT, mPrompt
Select Case mbFlags
Case vbAbortRetryIgnore
SetDlgItemText wParam, IDABORT, But1
SetDlgItemText wParam, IDRETRY, But2
SetDlgItemText wParam, IDIGNORE, But3
Case vbYesNoCancel
SetDlgItemText wParam, IDYES, But1
SetDlgItemText wParam, IDNO, But2
SetDlgItemText wParam, IDCANCEL, But3
Case vbOKOnly
SetDlgItemText wParam, IDOK, But1
Case vbRetryCancel
SetDlgItemText wParam, IDRETRY, But1
SetDlgItemText wParam, IDCANCEL, But2
Case vbYesNo
SetDlgItemText wParam, IDYES, But1
SetDlgItemText wParam, IDNO, But2
Case vbOKCancel
SetDlgItemText wParam, IDOK, But1
SetDlgItemText wParam, IDCANCEL, But2
End Select
UnhookWindowsHookEx MSGHOOK.hHook
End If
MsgBoxHookProc = False
End Function
Public Function BBmsgbox(mhwnd As Long, _
mMsgbox As VbMsgBoxStyle, Title As String, _
Prompt As String, Optional mMsgIcon As VbMsgBoxStyle, _
Optional ButA As String, Optional ButB As String, _
Optional ButC As String) As String
Dim CustomText As Long
mbFlags = mMsgbox
mbFlags2 = mMsgIcon
mTitle = Title
mPrompt = Prompt
But1 = ButA
But2 = ButB
But3 = ButC
CustomText = MessageBoxH(mhwnd, GetDesktopWindow(), mbFlags Or mbFlags2)
Select Case CustomText
Case IDABORT: BBmsgbox = But1
Case IDRETRY: BBmsgbox = But2
Case IDIGNORE: BBmsgbox = But3
Case IDYES: BBmsgbox = But1
Case IDNO: BBmsgbox = But2
Case IDCANCEL: BBmsgbox = But3
Case IDOK: BBmsgbox = But1
End Select
End Function
Sub TestYNC()
Const MY_YES As String = "Aye"
Const MY_NO As String = "Nay"
Dim DialogResult As String
DialogResult = BBmsgbox(1, vbYesNo, "[Title here]", "[Prompt here]", vbInformation, MY_YES, MY_NO)
Select Case DialogResult
Case MY_YES: MsgBox MY_YES & " = vbYes"
Case MY_NO: MsgBox MY_NO & " = vbNo"
End Select
End Sub
谢谢你们的及时回复。至少我不觉得自己很傻,因为我没有找到一个简单直接的解决方案来解决这个问题。我的英语和法语各占一半。过去在不同的机器上安装了法语版和英语版的MS Office,但最近MS将我切换到基于Web的365,他们决定我的个人资料需要法语区域/语言设置。我只是在一次接近最后的调试运行中发现了MsgBox Yes/No按钮blooper,由于在我的应用程序中有许多实例,我希望有一个简单的修复方法,而不是用UserForms替换所有的MsgBox。祷告微软有一天能提供一个简单的工具来编辑微软邮箱的按钮标题。a不会这样做。你需要一个。好吧,MsgBox函数可以通过API和回调来实现这一点,但是用户表单当然更容易。如果你真的想通过学习如何实现这一点来增强你的知识,那么请阅读MsgBox的子类化。如果只是一次性的,那么按照上面用户的建议去做。使用userform这似乎不是问题的答案。再看一看