Custome message box button text

I am trying to implement the attached file i founs elsewhere It crashes on windows 10 64bit and office 2016 64bit.I need the experts
CustomMsgbox.xlsm
SvgmassiveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
How does if fail?

Run the macro UnitTest1 and it works.
SvgmassiveAuthor Commented:
it excel crashes at the at
    m_lProcHook = SetWindowsHookEx(WH_CBT, _
                                   AddressOf MsgBoxHookProc, _
                                   m_hInstance, _
                                   m_hThreadID)
Roy CoxGroup Finance ManagerCommented:
What do you do just before the crash? I've displayed the mrssage box and clicked all the buttons with no crashing.
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Fabrice LambertConsultingCommented:
Experiencing crash here as well (Office 64).

What's this is supposed to do ?

Side note:
Give up hungarian notation, it provides nothing usefull, only make the code tougher to read.
SvgmassiveAuthor Commented:
this simplified version works.Lambert it allows you to change the button caption to what ever you wish.
msgbox.xlsb
Fabrice LambertConsultingCommented:
Well …….

Better write a custom form, and use it as a dialog box.
It will be easyer to undesrstand and more stable than messing up with APIs.

Side note:
I don't think doing correspondance between custom and VBA constants is a wise idea. They do not have the same meaning.
Roy CoxGroup Finance ManagerCommented:
I've played around with the original example and changed Titles, etc. No crashing for me.
CustomMsgbox.xlsm
SvgmassiveAuthor Commented:
Roy it just doesn't work for me i don't know what to say
Roy CoxGroup Finance ManagerCommented:
I'm using Office 365 ProPlus. It's 32bit but your API calls look as if they should work OK with 64bit.
SvgmassiveAuthor Commented:
i am using 2016 pro 64bit. it crashes everytime
Roy CoxGroup Finance ManagerCommented:
Can you test on 32 bit. Where did you find this msgbox?
SvgmassiveAuthor Commented:
it works on 32bit no problem.
here's the link
http://shutupdean.com/blog/2014/08/01/vba-msgbox-custom-button-text/
Rory ArchibaldCommented:
I don't think your declarations are correct for 64bit. (FindWindow is definitely wrong as it returns a window handle which must be a LongPtr, not Long).

Try:

Option Explicit

Private Const HCBT_ACTIVATE = 5

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

#If Win64 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As LongPtr) As Long
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As LongPtr)
    Private Declare PtrSafe Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As LongPtr, ByVal lpString As String) As LongPtr
    Private Declare PtrSafe Function RemoveProp Lib "user32" Alias "RemovePropA" (ByVal hwnd As LongPtr, ByVal lpString As String) As LongPtr
    Private Declare PtrSafe Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As LongPtr, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
#Else
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function RemoveProp Lib "user32" Alias "RemovePropA" (ByVal hwnd As Long, ByVal lpString As String) As Long
    Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
#End If

Private m_hWnd As Long
#If VBA7 Then
Public Property Get hWndApplication() As LongPtr
    m_hWnd = Application.hwnd
End Property
#Else
Public Property Get hWndApplication() As Long
    m_hWnd = Application.hwnd
End Property
#End If


Public Function MsgBoxHookProc(ByVal uMsg As Long, _
                                ByVal wParam As LongPtr, _
                                ByVal lParam As LongPtr) As LongPtr

    #If VBA7 Then
        Dim lPtr As LongPtr
        Dim lProcHook As LongPtr
    #Else
        Dim lPtr As Long
        Dim lProcHook As Long
    #End If
    
    Dim cM As clsMsgbox
    
    Select Case uMsg
        Case HCBT_ACTIVATE
            lPtr = GetProp(hWndApplication, "ObjPtr")
            If (lPtr <> 0) Then
                Set cM = ObjectFromPtr(lPtr)
                If Not cM Is Nothing Then
                    If Len(cM.ButtonText1) > 0 And Len(cM.ButtonText2) > 0 And Len(cM.ButtonText3) > 0 Then
                        If cM.UseCancel Then
                            SetDlgItemText wParam, IDYES, cM.ButtonText1
                            SetDlgItemText wParam, IDNO, cM.ButtonText2
                            SetDlgItemText wParam, IDCANCEL, cM.ButtonText3
                        Else
                            SetDlgItemText wParam, IDABORT, cM.ButtonText1
                            SetDlgItemText wParam, IDRETRY, cM.ButtonText2
                            SetDlgItemText wParam, IDIGNORE, cM.ButtonText3
                        End If
                        
                    ElseIf Len(cM.ButtonText1) > 0 And Len(cM.ButtonText2) Then
                        If cM.UseCancel Then
                            SetDlgItemText wParam, IDOK, cM.ButtonText1
                            SetDlgItemText wParam, IDCANCEL, cM.ButtonText2
                        Else
                            SetDlgItemText wParam, IDYES, cM.ButtonText1
                            SetDlgItemText wParam, IDNO, cM.ButtonText2
                        End If
                    Else
                        SetDlgItemText wParam, IDOK, cM.ButtonText1
                    End If
                    lProcHook = cM.ProcHook
                End If
            End If
            RemovePropPointer
            If lProcHook <> 0 Then UnhookWindowsHookEx lProcHook
    End Select
    
    MsgBoxHookProc = False
End Function
#If VBA7 Then
    Private Property Get ObjectFromPtr(ByVal lPtr As LongPtr) As Object
        Dim obj As Object
        
        CopyMemory obj, lPtr, Len(lPtr)
        Set ObjectFromPtr = obj
        CopyMemory obj, 0&, Len(lPtr)
    End Property
#Else
    Private Property Get ObjectFromPtr(ByVal lPtr As Long) As Object
        Dim obj As Object
        
        CopyMemory obj, lPtr, Len(lPtr)
        Set ObjectFromPtr = obj
        CopyMemory obj, 0&, Len(lPtr)
    End Property
#End If
Public Sub RemovePropPointer()
    #If Win64 Then
        Dim lPtr As LongPtr
    #Else
        Dim lPtr As Long
    #End If
    
    lPtr = GetProp(hWndApplication, "ObjPtr")
    If lPtr <> 0 Then RemoveProp hWndApplication, "ObjPtr"
End Sub

Open in new window


I don't have 64bit here to test currently. As written this will only work on 64bit - you'd need conditional compilation for the callback declaration too if you want it to work on both.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SvgmassiveAuthor Commented:
Building on what Rory mention with a few modification.It's up and running in 64bit.
Thanks to everyone
CustomMsgboxFixed.xlsb
SvgmassiveAuthor Commented:
Haven't tried it in a 32 bit environment
Roy CoxGroup Finance ManagerCommented:
Still works fine in 32 Bit
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 10

From novice to tech pro — start learning today.