I am writing a Java program that interacts with Microsoft Outlook using the Jacob library (bridges COM and Java). This program creates a new MailItem, displaying its Inspector window to the user. I wish to subscribe to the inspector's Close event to know when the user is finished editing their mail item.
To subscribe to the event, I followed the instructions in Jacob's documentation (about 2⁄3 down the page):
The current [event] model is conceptually
similar to the Visual Basic WithEvents
construct. Basically, I provide a
class called
com.jacob.com.DispatchEvents which has
a constructor that takes a source
object (of type
com.jacob.com.Dispatch) and a target
object (of any type). The source
object is queried for its
IConnectionPointContainer interface
and I attempt to obtain an
IConnectionPoint for its default
source interface (which I obtain from
IProvideClassInfo). At the same time,
I also create a mapping of DISPID's
for the default source interface to
the actual method names. I then use
the method names to get jmethodID
handles from the target Java object.
All event methods currently must have
the same signature: one argument which
is a Java array of Variants, and a
void return type.
Here is my InspectorEventHandler class, conforming to Jacob's documentation:
public class InspectorEventHandler {
public void Activate(Variant[] arguments) {
}
public void BeforeMaximize(Variant[] arguments) {
}
public void BeforeMinimize(Variant[] arguments) {
}
public void BeforeMove(Variant[] arguments) {
}
public void BeforeSize(Variant[] arguments) {
}
public void Close(Variant[] arguments) {
System.out.println("Closing");
}
public void Deactivate(Variant[] arguments) {
}
public void PageChange(Variant[] arguments) {
}
}
And here is how I subscribe to the events using this InspectorEventHandler class:
Object outlook = new ActiveXComponent("Outlook.Application");
Object mailItem = Dispatch.call(outlook, "CreateItem", 0).getDispatch();
Object inspector = Dispatch.get(mailItem, "GetInspector").getDispatch();
InspectorEventHandler eventHandler = new InspectorEventHandler();
// This supposedly registers eventHandler with the inspector
new DispatchEvents((Dispatch) inspector, eventHandler);
However, the last line fails with the following exception:
Exception in thread "main" com.jacob.com.ComFailException: Can't find event iid
at com.jacob.com.DispatchEvents.init(Native Method)
at com.jacob.com.DispatchEvents.(DispatchEvents.java)
at cake.CakeApplication.run(CakeApplication.java:30)
at cake.CakeApplication.main(CakeApplication.java:15)
couldn't get IProvideClassInfo
According to Google, a few others have also received this error. Unfortunately, none of them have received an answer.
I am using version 1.7 of the Jacob library, which claims to prevent this problem:
Version 1.7 also includes code to read
the type library directly from the
progid. This makes it possible to work
with all the Microsoft Office
application events, as well as IE5
events. For an example see the
samples/test/IETest.java example.
I noticed that the aforementioned IETest.java file subscribes to events like this:
new DispatchEvents((Dispatch) ieo, ieE,"InternetExplorer.Application.1");
Therefore, I tried subscribing to my events in a similar manner:
new DispatchEvents((Dispatch) inspector, eventHandler, "Outlook.Application");
new DispatchEvents((Dispatch) inspector, eventHandler, "Outlook.Application.1");
new DispatchEvents((Dispatch) inspector, eventHandler, "Outlook.Application.12");
All these attempts failed with the same error.