views:

95

answers:

1

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?

+3  A: 

Apparently Events are not allowed to be passed through an interface class into the concrete class like you want to using "Implements". In this article it states: "Event declarations of the abstract interface are not included in the interface that is inherited by concrete classes. I haven't found anywhere that this has been acknowledged as a bug; however, it does seem to be one."

Here is the link to the source: http://www.devx.com/getHelpOn/10MinuteSolution/20416

:-(

Fink
@Fink: Thanks for that; looks like it's a bug all right. I wish the MSDN hadn't gotten my hopes up! Still, I think I have a workaround, though using events would have been more elegant. C'est la vie.
Ken Keenan