Hello,
Is it possible to change the caption of a msgbox button?
I want to change "Cancel" in "Not now", instead of making a userform to solve this.
kind regards
Willem
As far as I know it's not possible.
Aflatoon
09-22-2015, 05:10 AM
It is possible, but a userform is a lot simpler:
Option ExplicitPrivate Const MB_YESNOCANCEL = &H3&
Private Const MB_SYSTEMMODAL As Long = &H1000&
Public Const IDCANCEL = 2
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
Private Type MSGBOX_HOOK_PARAMS
hOwner As Long
hHook As Long
End Type
Private MSGHOOK As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () 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 UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Function CustomMessageBox(sPrompt As String, sTitle As String, lOptions As Long) As Long
Dim hInstance As Long
Dim hThreadId As Long
Dim hOwner As Long
hThreadId = GetCurrentThreadId()
hInstance = GetWindowLong(hThreadId, GWL_HINSTANCE)
hOwner = Application.hWnd
With MSGHOOK
.hOwner = hOwner
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
End With
CustomMessageBox = MessageBox(hOwner, sPrompt, sTitle, lOptions)
End Function
Public Function MsgBoxHookProc(ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If uMsg = HCBT_ACTIVATE Then
SetDlgItemText wParam, IDCANCEL, "Not now"
UnhookWindowsHookEx MSGHOOK.hHook
End If
MsgBoxHookProc = False
End Function
Sub testcall()
Dim var
var = CustomMessageBox("Your message here", "Your title", MB_YESNOCANCEL + MB_SYSTEMMODAL)
End Sub
Yes, indeed :)
I keep the default buttons.
Thanks a lot!
kind regards
Willem
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.