Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
#If VBA7 Then
Private Declare PtrSafe Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, lpiid As GUID) As Long
Private Declare PtrSafe Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" (ByVal punk As stdole.IUnknown, ByRef riidEvent As GUID, ByVal fConnect As Long, ByVal punkTarget As stdole.IUnknown, ByRef pdwCookie As Long, Optional ByVal ppcpOut As LongPtr) As Long
Private Declare PtrSafe Function IsWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As Long
Private hwnd As LongPtr
#Else
Private Declare Function IIDFromString Lib "ole32.dll" (ByVal lpsz As Long, lpiid As GUID) As Long
Private Declare Function ConnectToConnectionPoint Lib "shlwapi" Alias "#168" (ByVal punk As stdole.IUnknown, ByRef riidEvent As GUID, ByVal fConnect As Long, ByVal punkTarget As stdole.IUnknown, ByRef pdwCookie As Long, Optional ByVal ppcpOut As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As Long) As Long
Private hwnd As Long
#End If
Private WithEvents CmndBras As CommandBars
Private oClientForm As Object
Private oCurrentTextBox As MSForms.TextBox
Private sClassInstanceName As String
Event OnEnter(ByVal TextBox As MSForms.TextBox)
Event OnExit(ByVal TextBox As MSForms.TextBox, ByRef Cancel As MSForms.ReturnBoolean)
Event BeforeUpdate(ByVal TextBox As MSForms.TextBox, ByRef Cancel As MSForms.ReturnBoolean)
Event AfterUpdate(ByVal TextBox As MSForms.TextBox)
' __________________________________ CLASS PUBLIC METHOD ________________________________________
Public Property Let HookEvents(ClassInstanceName As String, Optional ByVal TextBox As MSForms.TextBox, ByVal SetEvents As Boolean)
Const S_OK = &H0
Static lCookie As Long
Dim tIID As GUID
If Not TextBox Is Nothing Then
Set oCurrentTextBox = TextBox
Set oClientForm = GetUserForm(TextBox)
sClassInstanceName = ClassInstanceName
Set CmndBras = Application.CommandBars
Call IUnknown_GetWindow(oClientForm, VarPtr(hwnd))
End If
If IIDFromString(StrPtr("{00020400-0000-0000-C000-000000000046}"), tIID) = S_OK Then
If ConnectToConnectionPoint(Me, tIID, SetEvents, TextBox, lCookie) = S_OK Then
'Debug.Print oCurrentTextBox.Name & IIf(SetEvents, " connected ", " disconnected") & " successfully."
Else
'Debug.Print "Connection failed for: " & oCurrentTextBox.Name
End If
End If
End Property
' __________________________________ TEXTBOX CONTROL EVENTS ________________________________________
Public Sub OnEnter()
'Attribute OnEnter.VB_UserMemId = &H80018202
Dim oThis As CTextBoxEvents
Set oThis = Me
Call CallByName(oClientForm, sClassInstanceName, VbSet, oThis)
Set oThis = Nothing
RaiseEvent OnEnter(oCurrentTextBox)
End Sub
Public Sub OnExit(ByVal Cancel As MSForms.ReturnBoolean)
'Attribute OnExit.VB_UserMemId = &H80018203
RaiseEvent OnExit(oCurrentTextBox, Cancel)
End Sub
Public Sub BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
'Attribute BeforeUpdate.VB_UserMemId = &H80018201
RaiseEvent BeforeUpdate(oCurrentTextBox, Cancel)
End Sub
Public Sub AfterUpdate()
'Attribute AfterUpdate.VB_UserMemId = &H80018200
RaiseEvent AfterUpdate(oCurrentTextBox)
End Sub
' __________________________________ PRIVATE ROUTINES ________________________________________
Private Function GetUserForm(ByVal Ctrl As MSForms.Control) As Object
Dim oTmp As Object
Set oTmp = Ctrl.Parent
Do While TypeOf oTmp Is MSForms.Control
Set oTmp = oTmp.Parent
Loop
Set GetUserForm = oTmp
End Function
Private Sub CmndBras_OnUpdate()
If IsWindow(hwnd) = 0 Then
HookEvents(sClassInstanceName, oCurrentTextBox) = False
End If
End Sub
Private Sub Class_Terminate()
'Debug.Print "Class instance treminated and memory released properly related to: " & oCurrentTextBox.Name
Set oCurrentTextBox = Nothing: Set oClientForm = Nothing: Set CmndBras = Nothing
End Sub