I can not trigger OLEObject.GotFocus event by ConnectToConnectionPoint API

loquat

New Member
Joined
Feb 24, 2024
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
VBA Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Option Explicit

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private cookie As Long
Private iid As GUID
Private cls2 As Class2

Private Sub Class_Initialize()
    'OLEObjects Events
    Const s = "{00024410-0000-0000-C000-000000000046}"
    
    Dim hr As Long
    hr = IIDFromString(StrPtr(s), iid)
    Debug.Print hr

    Set cls2 = New Class2
    hr = ConnectToConnectionPoint(cls2, iid, 1, ie, cookie)
    Debug.Print Hex(hr), cookie   '[B][COLOR=rgb(226, 80, 65)]Error code: hr = 80004005, and cookie = 0[/COLOR][/B]
    If hr <> 0 Then Exit Sub

End Sub

Private Sub Class_Terminate()
    Dim hr As Long
    hr = ConnectToConnectionPoint(Nothing, iid, 0, ie, cookie)
    Debug.Print Hex(hr), cookie
    If hr <> 0 Then Exit Sub
End Sub
VBA Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

Public Sub DocumentComplete(ByVal pDisp As Object, URL As Variant)  'the original event has no parameters
Attribute DocumentComplete.VB_UserMemId = 259
    Debug.Print "DocumentComplete"
End Sub

Public Sub myGotFocus()
Attribute myGotFocus.VB_UserMemId = 1541
    Debug.Print "myGotFocus"
End Sub

Public Sub myLostFocus()
Attribute myLostFocus.VB_UserMemId = 1542
    Debug.Print "myLostFocus"
End Sub

when i call it like this, i get an error code of 80004005, in Class1_Initialize
VBA Code:
Private cls As Class1

Sub start_()
    Set cls = New Class1
End Sub

Sub end_()
    Set cls = Nothing
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,883
Messages
6,175,168
Members
452,615
Latest member
bogeys2birdies

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