vbYesNo msgbox - change button captions

Gives

Board Regular
Joined
Aug 22, 2006
Messages
160
Can the msgbox vbYesNo button captions be changed - for example change "Yes" to "Import" and "No" to "Export" so that clicking "Import" would return vbYes and clicking "Export" would return vbNo?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Use Hook:
Code:
Private sButton1 As String
Private sButton2 As String
Private sCaption As String
Private sText As String
Private Const MB_ICONQUESTION As Long = &H20&
Private Const MB_OKCANCEL As Long = &H1&
Private Const MB_TASKMODAL As Long = &H2000&
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
Private 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

Private Function myMessageBox(hwndThreadOwner As Long, hwndOwner As Long, strCaption As String, strText As String, strButton1 As String, strButton2 As String) As Long
    sButton1 = strButton1
    sButton2 = strButton2
    sCaption = strCaption
    sText = strText
    Dim hInstance As Long, hThreadId As Long
     hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
    hThreadId = GetCurrentThreadId()
    With MSGHOOK
        .hwndOwner = hwndOwner
        .hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
    End With
    myMessageBox = MessageBox(hwndOwner, Space$(120), Space$(120), MB_OKCANCEL Or MB_ICONQUESTION)
End Function
Private Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = HCBT_ACTIVATE Then
        SetWindowText wParam, sCaption
        SetDlgItemText wParam, IDOK, sButton1
        SetDlgItemText wParam, IDCANCEL, sButton2
        SetDlgItemText wParam, IDPROMPT, sText
        UnhookWindowsHookEx MSGHOOK.hHook
    End If
    MsgBoxHookProc = False
End Function

Sub Macro1()
Dim msg As Long
msg = myMessageBox(0, GetDesktopWindow(), "Question", "Which one do you love,Word or Excel?", "Excel", "Word")
If msg = IDOK Then myMessageBox 0, GetDesktopWindow(), "I love Excel", "Which Version?", "Excel 97", "Excel 2007"
If msg = IDCANCEL Then myMessageBox 0, GetDesktopWindow(), "I love Word", "Which Version?", "Word 97", "Word 2007"
End Sub

Best Regards
Northwolves
 
Upvote 0
Wow! I like the myMessageBox function but the prgramming is over my head. I'll need to study up o understand what all the Private Declare Function statements do.

I can create the suggested userform and use the desired captions in the call arguments.

Thanks for the suggestions.
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,153
Latest member
JazzSingerNL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top