Change Yes / No in Vba

MsgBox " Select Any of the two Options "
MyNote = "Which type of file ?"
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "")
If Answer = vbNo Then
    Call A()
Else 
    Call B()

everything works correctly, but I just want to change the appearance of the yes / no options for A and B, so that the user better understands which option to choose for which file.

+3
source share
3 answers

As noted, you can use the API as discusse HERE .

I sent the code below just in case the link is disabled and your code has also been added. NTN.

Option Explicit

' Import
Private Declare Function GetCurrentThreadId Lib "kernel32" () 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

' Handle to the Hook procedure
Private hHook As Long

' Hook type
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5

' Constants
Public Const IDOK = 1
Public Const IDCANCEL = 2
Public Const IDABORT = 3
Public Const IDRETRY = 4
Public Const IDIGNORE = 5
Public Const IDYES = 6
Public Const IDNO = 7

Public Sub Test()
    ' Set Hook
    hHook = SetWindowsHookEx(WH_CBT, _
                             AddressOf MsgBoxHookProc, _
                             0, _
                             GetCurrentThreadId)

    ' Run MessageBox
    MyNote = "Which type of file ?"
    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Select Any of the two options")
    If Answer = vbNo Then Call A() Else Call B()

End Sub

Private Function MsgBoxHookProc(ByVal lMsg As Long, _
                                ByVal wParam As Long, _
                                ByVal lParam As Long) As Long

    If lMsg = HCBT_ACTIVATE Then
        SetDlgItemText wParam, IDYES, "A" '~~> replacement for Yes
        SetDlgItemText wParam, IDNO, "B" '~~> replacement for No

        ' Release the Hook
        UnhookWindowsHookEx hHook
    End If

    MsgBoxHookProc = False
End Function

If you are using 64 bits, you need to add PtrSafe .

+4
source

, , - . Al, "", . Case , ..

Dim ibox
Retryinput:
ibox = inputBox("Choose option 1 or option 2")
Select case ibox
    Case 1
        Call A()
    Case 2
        Call B()
    Case Else
        If MsgBox("please enter 1 or 2", vbRetryCancel) = vbRetry then goto Retryinput
        Exit sub

End Select
+1

2 , , , Hook. , , 50 . , , , , ... , @kevinarpe, , , , , Immediate VBE... , , , Excel , . , , Immediate, , SetDlgItemText - hWnd msgbox. , , ... , , , ... , Hook ! Win8.1, 32bit-Excel2010. , VBE, : How to change button names in a message box?

0
source

Source: https://habr.com/ru/post/1731435/


All Articles