Patch 1208570 added parameters that let a user provide typelib information required to get Excel events
This commit is contained in:
@@ -6,45 +6,79 @@ import com.jacob.com.DispatchEvents;
|
||||
|
||||
/**
|
||||
* This test was lifted from a forum posting and shows how you can't listen to
|
||||
* Excel events added post 1.9.1 Eclipse Settings...
|
||||
* Excel events (added post 1.9.1 Eclipse Settings.) This also uses the 1.9.1
|
||||
* InvocationProxy to receive the events.
|
||||
* <p> supported command line options with default values are
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=true
|
||||
* -Dcom.jacob.debug=false
|
||||
*/
|
||||
public class ExcelEventTest {
|
||||
public class ExcelEventTest extends InvocationProxy {
|
||||
|
||||
/**
|
||||
* load up excel, register for events and make stuff happen
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
|
||||
listenTo("Word.Application",null);
|
||||
|
||||
// Create an Excel Listener
|
||||
listenTo("Excel.Application",
|
||||
"C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE");
|
||||
}
|
||||
|
||||
private static void listenTo(String pid, String typeLibLocation) {
|
||||
String pid = "Excel.Application";
|
||||
String typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE";
|
||||
|
||||
// Grab The Component.
|
||||
ActiveXComponent axc = new ActiveXComponent(pid);
|
||||
try {
|
||||
// Add a listener (doesn't matter what it is).
|
||||
DispatchEvents de;
|
||||
if (typeLibLocation == null){
|
||||
if (typeLibLocation == null) {
|
||||
de = new DispatchEvents(axc, new ExcelEventTest());
|
||||
} else {
|
||||
de = new DispatchEvents(axc, new ExcelEventTest(),
|
||||
pid,typeLibLocation);
|
||||
de = new DispatchEvents(axc, new ExcelEventTest(), pid,
|
||||
typeLibLocation);
|
||||
}
|
||||
if (de == null){
|
||||
System.out.println(
|
||||
"No exception thrown but now dispatch returned for Excel events");
|
||||
if (de == null) {
|
||||
System.out
|
||||
.println("No exception thrown but no dispatch returned for Excel events");
|
||||
} else {
|
||||
// Yea!
|
||||
System.out.println("Successfully attached to " + pid);
|
||||
|
||||
}
|
||||
// Yea!
|
||||
System.out.println("Successfully attached to " + pid);
|
||||
} catch (ComFailException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
System.out.println("version=" + axc.getProperty("Version"));
|
||||
System.out.println("version=" + Dispatch.get(axc, "Version"));
|
||||
axc.setProperty("Visible", true);
|
||||
Dispatch workbooks = axc.getPropertyAsComponent("Workbooks");
|
||||
Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
|
||||
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
|
||||
Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { "A1" }, new int[1]).toDispatch();
|
||||
Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { "A2" }, new int[1]).toDispatch();
|
||||
Dispatch.put(a1, "Value", "123.456");
|
||||
Dispatch.put(a2, "Formula", "=A1*2");
|
||||
System.out.println("Retrieved a1 from excel:" + Dispatch.get(a1, "Value"));
|
||||
System.out.println("REtrieved a2 from excel:" + Dispatch.get(a2, "Value"));
|
||||
Variant f = new Variant(false);
|
||||
Dispatch.call(workbook, "Close", f);
|
||||
axc.invoke("Quit", new Variant[] {});
|
||||
|
||||
} catch (ComFailException cfe) {
|
||||
cfe.printStackTrace();
|
||||
System.out.println("Failed to attach to " + pid + ": "
|
||||
+ e.getMessage());
|
||||
+ cfe.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dummy consturctor to create an InvocationProxy that wraps nothing
|
||||
*/
|
||||
public ExcelEventTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* override the invoke method to loga ll the events
|
||||
*/
|
||||
public void invoke(String methodName, Variant targetParameter[]) {
|
||||
System.out.println("Received event from Windows program" + methodName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
75
unittest/com/jacob/com/WordEventTest.java
Normal file
75
unittest/com/jacob/com/WordEventTest.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package com.jacob.com;
|
||||
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.ComFailException;
|
||||
import com.jacob.com.DispatchEvents;
|
||||
|
||||
/**
|
||||
* This test was lifted from a forum posting and shows how you can't listen to
|
||||
* Excel events (added post 1.9.1 Eclipse Settings.) This also uses the 1.9.1
|
||||
* InvocationProxy to receive the events.
|
||||
* <p> supported command line options with default values are
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=false
|
||||
*/
|
||||
public class WordEventTest extends InvocationProxy {
|
||||
|
||||
/**
|
||||
* load up excel, register for events and make stuff happen
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
String pid = "Word.Application";
|
||||
String typeLibLocation = null;
|
||||
|
||||
// Grab The Component.
|
||||
ActiveXComponent axc = new ActiveXComponent(pid);
|
||||
try {
|
||||
// Add a listener (doesn't matter what it is).
|
||||
DispatchEvents de;
|
||||
if (typeLibLocation == null) {
|
||||
de = new DispatchEvents(axc, new WordEventTest());
|
||||
} else {
|
||||
de = new DispatchEvents(axc, new WordEventTest(), pid,
|
||||
typeLibLocation);
|
||||
}
|
||||
if (de == null) {
|
||||
System.out
|
||||
.println("No exception thrown but no dispatch returned for Word events");
|
||||
} else {
|
||||
// Yea!
|
||||
System.out.println("Successfully attached to " + pid);
|
||||
|
||||
}
|
||||
// this is different from the ExcelEventTest because it uses
|
||||
// the jacob active X api instead of the Dispatch api
|
||||
System.out.println("version=" + axc.getPropertyAsString("Version"));
|
||||
axc.setProperty("Visible",true);
|
||||
ActiveXComponent documents = axc.getPropertyAsComponent("Documents");
|
||||
if (documents == null){
|
||||
System.out.println("unable to get documents");
|
||||
}
|
||||
axc.invoke("Quit", new Variant[] {});
|
||||
|
||||
} catch (ComFailException cfe) {
|
||||
cfe.printStackTrace();
|
||||
System.out.println("Failed to attach to " + pid + ": "
|
||||
+ cfe.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dummy consturctor to create an InvocationProxy that wraps nothing
|
||||
*/
|
||||
public WordEventTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* override the invoke method to loga ll the events
|
||||
*/
|
||||
public void invoke(String methodName, Variant targetParameter[]) {
|
||||
System.out.println("Received event from Windows program" + methodName);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user