Run-time error 459 when using WithEvents with a class that implements another

Posted by Ken Keenan on Stack Overflow See other posts from Stack Overflow or by Ken Keenan
Published on 2010-06-11T12:58:24Z Indexed on 2010/06/11 16:02 UTC
Read the original article Hit count: 248

Filed under:
|
|
|

I am developing a VBA project in Word and have encountered a problem with handling events when using a class that implements another.

I define an empty class, IMyInterface:

Public Sub Xyz()
End Sub

Public Event SomeEvent()

And a class, MyClass that implements the above:

Implements IMyInterface

Public Event SomeEvent()

Public Sub Xyz()
  ' ... code ...
  RaiseEvent SomeEvent
End Sub

Private Sub IMyInterface_Xyz()
   Xyz
End  Sub

If I create a third class, OtherClass, that declares a member variable with the type of the interface class:

Private WithEvents mMy As IMyInterface

and try to initialize this variable with an instance of the implementing class:

Set mMy = New MyClass

I get a run-time error '459': This component doesn't support this set of events.

The MSDN page for this error message states:

"You tried to use a WithEvents variable with a component that can't work as an event source for the specified set of events. For example, you may be sinking events of an object, then create another object that Implements the first object. Although you might think you could sink the events from the implemented object, that isn't automatically the case. Implements only implements an interface for methods and properties."

The above pretty much sums up what I'm trying to do. The wording, "that isn't automatically the case", rather than "this is flat-out impossible", seems to suggest that there is some bit of manual work I need to do to get it to work, but it doesn't tell me what! Does anybody know if this is possible in VBA?

© Stack Overflow or respective owner

Related posts about vba

Related posts about msword