Custome message box button text

Svgmassive
Svgmassive used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager

Commented:
How does if fail?

Run the macro UnitTest1 and it works.

Author

Commented:
it excel crashes at the at
    m_lProcHook = SetWindowsHookEx(WH_CBT, _
                                   AddressOf MsgBoxHookProc, _
                                   m_hInstance, _
                                   m_hThreadID)
Roy CoxGroup Finance Manager

Commented:
What do you do just before the crash? I've displayed the mrssage box and clicked all the buttons with no crashing.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
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.

Author

Commented:
this simplified version works.Lambert it allows you to change the button caption to what ever you wish.
msgbox.xlsb
Fabrice LambertConsulting
Distinguished Expert 2017

Commented:
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 Manager

Commented:
I've played around with the original example and changed Titles, etc. No crashing for me.
CustomMsgbox.xlsm

Author

Commented:
Roy it just doesn't work for me i don't know what to say
Roy CoxGroup Finance Manager

Commented:
I'm using Office 365 ProPlus. It's 32bit but your API calls look as if they should work OK with 64bit.

Author

Commented:
i am using 2016 pro 64bit. it crashes everytime
Roy CoxGroup Finance Manager

Commented:
Can you test on 32 bit. Where did you find this msgbox?

Author

Commented:
it works on 32bit no problem.
here's the link
http://shutupdean.com/blog/2014/08/01/vba-msgbox-custom-button-text/
Most Valuable Expert 2011
Top Expert 2011
Commented:
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.

Author

Commented:
Building on what Rory mention with a few modification.It's up and running in 64bit.
Thanks to everyone
CustomMsgboxFixed.xlsb

Author

Commented:
Haven't tried it in a 32 bit environment
Roy CoxGroup Finance Manager

Commented:
Still works fine in 32 Bit

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial