Excel interop: _Worksheet or Worksheet?

If I recall correctly — and my memory on this is a bit fuzzy, it has been a long time since I took the Excel PIA apart — it’s like this.

An event is essentially a method that an object calls when something happens. In .NET, events are delegates, plain and simple. But in COM, it is very common to organize a whole bunch of event callbacks into interfaces. You therefore have two interfaces on a given object — the “incoming” interface, the methods you expect other people to call on you, and the “outgoing” interface, the methods you expect to call on other people when events happen.

In the unmanaged metadata — the type library — for a creatable object there are definitions for three things: the incoming interface, the outgoing interface, and the coclass, which says “I’m a creatable object that implements this incoming interface and this outgoing interface”.

Now when the type library is automatically translated into metadata, those relationships are, sadly, preserved. It would have been nicer to have a hand-generated PIA that made the classes and interfaces conform more to what we’d expect in the managed world, but sadly, that didn’t happen. Therefore the Office PIA is full of these seemingly odd duplications, where every creatable object seems to have two interfaces associated with it, with the same stuff on them. One of the interfaces represents the interface to the coclass, and one of them represents the incoming interface to that coclass.

The _Workbook interface is the incoming interface on the workbook coclass. The Workbook interface is the interface which represents the coclass itself, and therefore inherits from _Workbook.

Long story short, I would use Workbook if you can do so conveniently; _Workbook is a bit of an implementation detail.

Leave a Comment