Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Excel event handling solution
Excel event handling solution [message #461479] Mon, 26 September 2005 09:14 Go to next message
Iwan Birrer is currently offline Iwan BirrerFriend
Messages: 17
Registered: July 2009
Junior Member
Hello,

I've read a couple of threads in this forum about listening to Microsoft
Excel events . However, nobody seems to have a proven solution yet. The
bug report (https://bugs.eclipse.org/bugs/show_bug.cgi?id=71348) has not
been updated for about a year.

Is anybody looking into this or even has a solution? Or does anybody
know another tool/library that supports listening to Excel events from Java?

Thanks,
Iwan
Re: Excel event handling solution [message #461825 is a reply to message #461479] Sat, 01 October 2005 01:10 Go to previous messageGo to next message
Toshihiro Izumi is currently offline Toshihiro IzumiFriend
Messages: 360
Registered: July 2009
Location: Japan
Senior Member
Here is a SAMPLE for handling Excel Application events.

ExcelAppEvents.java
--------
package org.eclipse.swt.ole.win32;

import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.COMObject;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.ole.win32.IDispatch;

public class ExcelAppEvents {

public final static GUID IID_AppEvents =
COMObject.IIDFromString("{00024413-0000-0000-C000-000000000046} ");
// Event ID
public final static int NewWorkbook = 0x0000061d;
public final static int SheetSelectionChange = 0x00000616;
public final static int SheetBeforeDoubleClick = 0x00000617;
public final static int SheetBeforeRightClick = 0x00000618;
public final static int SheetActivate = 0x00000619;
public final static int SheetDeactivate = 0x0000061a;
public final static int SheetCalculate = 0x0000061b;
public final static int SheetChange = 0x0000061c;
public final static int WorkbookOpen = 0x0000061f;
public final static int WorkbookActivate = 0x00000620;
public final static int WorkbookDeactivate = 0x00000621;
public final static int WorkbookBeforeClose = 0x00000622;
public final static int WorkbookBeforeSave = 0x00000623;
public final static int WorkbookBeforePrint = 0x00000624;
public final static int WorkbookNewSheet = 0x00000625;
public final static int WorkbookAddinInstall = 0x00000626;
public final static int WorkbookAddinUninstall = 0x00000627;
public final static int WindowResize = 0x00000612;
public final static int WindowActivate = 0x00000614;
public final static int WindowDeactivate = 0x00000615;
public final static int SheetFollowHyperlink = 0x0000073e;

private static IDispatch getApplication(OleControlSite site) {
IDispatch application = null;
String progID = site.getProgramID();
if (progID.startsWith("Excel.Sheet.")) {
OleAutomation excelSheet = new OleAutomation(site);
int[] dispIDs = excelSheet.getIDsOfNames(new String[]
{"Application"});
Variant pVarResult = excelSheet.getProperty(dispIDs[0]);
if (pVarResult != null && pVarResult.getType() !=
COM.VT_EMPTY) {
application = pVarResult.getDispatch();
}
excelSheet.dispose();
excelSheet = null;
} else { // I don't care.
}
return application;
}

public static void addEventListener(OleControlSite site, int eventID,
OleListener listener) {
IDispatch excelApp = getApplication(site);
if (excelApp != null) {
site.addEventListener(excelApp.getAddress(), IID_AppEvents,
eventID, listener);
}
}

public static void removeEventListener(OleControlSite site, int
eventID, OleListener listener) {
IDispatch excelApp = getApplication(site);
if (excelApp != null) {
site.removeEventListener(excelApp.getAddress(), IID_AppEvents,
eventID, listener);
}
}

}
--------

Excel.java
--------
package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.ole.win32.ExcelAppEvents;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Excel {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
OleFrame frame = new OleFrame(shell, SWT.NONE);
OleControlSite site = new OleControlSite(frame, SWT.NONE,
"Excel.Sheet");
site.doVerb(OLE.OLEIVERB_OPEN);

// Event WorkbookBeforeClose(Wb As Workbook, Cancel As Boolean)
ExcelAppEvents.addEventListener(site,
ExcelAppEvents.WorkbookBeforeClose, new OleListener() {
public void handleEvent(OleEvent event) {
System.out.println("WorkbookBeforeClose");
Variant varWorkbook = event.arguments[1];
//Variant varCancel = event.arguments[0];
varWorkbook.dispose(); //Object must be disposed
}
});
// Event WorkbookBeforeSave(Wb As Workbook, SaveAsUI As Boolean,
Cancel As Boolean)
ExcelAppEvents.addEventListener(site,
ExcelAppEvents.WorkbookBeforeSave, new OleListener() {
public void handleEvent(OleEvent event) {
System.out.println("WorkbookBeforeSave");
Variant varWorkbook = event.arguments[2];
//Variant varSaveAsUI = event.arguments[1];
Variant varCancel = event.arguments[0];
varCancel.setByRef(true); //Cancel for example
varWorkbook.dispose(); //Object must be disposed
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

}
--------

(tested with Excel2000 only)
Re: Excel event handling solution [message #461851 is a reply to message #461825] Mon, 03 October 2005 09:02 Go to previous messageGo to next message
Iwan Birrer is currently offline Iwan BirrerFriend
Messages: 17
Registered: July 2009
Junior Member
Thanks a lot. I've figured out a similar example last week and it worked
well with Excel 2003. Has anyody ever thought about programming a code
generator that would read an idl file and produce similar code as in the
example given by Toshihiro. Should be easy enough..
Anybody knows if there's a free parser for idl?

Iwan

Toshihiro Izumi wrote:
> Here is a SAMPLE for handling Excel Application events.
>
> ExcelAppEvents.java
> --------
> package org.eclipse.swt.ole.win32;
>
> import org.eclipse.swt.internal.ole.win32.COM;
> import org.eclipse.swt.internal.ole.win32.COMObject;
> import org.eclipse.swt.internal.ole.win32.GUID;
> import org.eclipse.swt.internal.ole.win32.IDispatch;
>
> public class ExcelAppEvents {
>
> public final static GUID IID_AppEvents =
> COMObject.IIDFromString("{00024413-0000-0000-C000-000000000046} ");
> // Event ID
> public final static int NewWorkbook = 0x0000061d;
> public final static int SheetSelectionChange = 0x00000616;
> public final static int SheetBeforeDoubleClick = 0x00000617;
> public final static int SheetBeforeRightClick = 0x00000618;
> public final static int SheetActivate = 0x00000619;
> public final static int SheetDeactivate = 0x0000061a;
> public final static int SheetCalculate = 0x0000061b;
> public final static int SheetChange = 0x0000061c;
> public final static int WorkbookOpen = 0x0000061f;
> public final static int WorkbookActivate = 0x00000620;
> public final static int WorkbookDeactivate = 0x00000621;
> public final static int WorkbookBeforeClose = 0x00000622;
> public final static int WorkbookBeforeSave = 0x00000623;
> public final static int WorkbookBeforePrint = 0x00000624;
> public final static int WorkbookNewSheet = 0x00000625;
> public final static int WorkbookAddinInstall = 0x00000626;
> public final static int WorkbookAddinUninstall = 0x00000627;
> public final static int WindowResize = 0x00000612;
> public final static int WindowActivate = 0x00000614;
> public final static int WindowDeactivate = 0x00000615;
> public final static int SheetFollowHyperlink = 0x0000073e;
>
> private static IDispatch getApplication(OleControlSite site) {
> IDispatch application = null;
> String progID = site.getProgramID();
> if (progID.startsWith("Excel.Sheet.")) {
> OleAutomation excelSheet = new OleAutomation(site);
> int[] dispIDs = excelSheet.getIDsOfNames(new String[]
> {"Application"});
> Variant pVarResult = excelSheet.getProperty(dispIDs[0]);
> if (pVarResult != null && pVarResult.getType() !=
> COM.VT_EMPTY) {
> application = pVarResult.getDispatch();
> }
> excelSheet.dispose();
> excelSheet = null;
> } else { // I don't care.
> }
> return application;
> }
>
> public static void addEventListener(OleControlSite site, int eventID,
> OleListener listener) {
> IDispatch excelApp = getApplication(site);
> if (excelApp != null) {
> site.addEventListener(excelApp.getAddress(), IID_AppEvents,
> eventID, listener);
> }
> }
>
> public static void removeEventListener(OleControlSite site, int
> eventID, OleListener listener) {
> IDispatch excelApp = getApplication(site);
> if (excelApp != null) {
> site.removeEventListener(excelApp.getAddress(),
> IID_AppEvents, eventID, listener);
> }
> }
>
> }
> --------
>
> Excel.java
> --------
> package test;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.ole.win32.ExcelAppEvents;
> import org.eclipse.swt.ole.win32.OLE;
> import org.eclipse.swt.ole.win32.OleControlSite;
> import org.eclipse.swt.ole.win32.OleEvent;
> import org.eclipse.swt.ole.win32.OleFrame;
> import org.eclipse.swt.ole.win32.OleListener;
> import org.eclipse.swt.ole.win32.Variant;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class Excel {
>
> public static void main(String[] args) {
> Display display = new Display();
> Shell shell = new Shell(display);
> OleFrame frame = new OleFrame(shell, SWT.NONE);
> OleControlSite site = new OleControlSite(frame, SWT.NONE,
> "Excel.Sheet");
> site.doVerb(OLE.OLEIVERB_OPEN);
>
> // Event WorkbookBeforeClose(Wb As Workbook, Cancel As Boolean)
> ExcelAppEvents.addEventListener(site,
> ExcelAppEvents.WorkbookBeforeClose, new OleListener() {
> public void handleEvent(OleEvent event) {
> System.out.println("WorkbookBeforeClose");
> Variant varWorkbook = event.arguments[1];
> //Variant varCancel = event.arguments[0];
> varWorkbook.dispose(); //Object must be disposed
> }
> });
> // Event WorkbookBeforeSave(Wb As Workbook, SaveAsUI As Boolean,
> Cancel As Boolean)
> ExcelAppEvents.addEventListener(site,
> ExcelAppEvents.WorkbookBeforeSave, new OleListener() {
> public void handleEvent(OleEvent event) {
> System.out.println("WorkbookBeforeSave");
> Variant varWorkbook = event.arguments[2];
> //Variant varSaveAsUI = event.arguments[1];
> Variant varCancel = event.arguments[0];
> varCancel.setByRef(true); //Cancel for example
> varWorkbook.dispose(); //Object must be disposed
> }
> });
>
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
> display.dispose();
> }
>
> }
> --------
>
> (tested with Excel2000 only)
>
>
Re: Excel event handling solution [message #462074 is a reply to message #461851] Wed, 05 October 2005 18:53 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The example given by Toshihiro uses internal classes and calls a non-public
API on OleControlSite.

In 3.2 I have added a new method to make it possible to add event listeners
for a specified event sink IID.

The following example shows how to do this:

http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet199.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup


"Iwan Birrer" <birrer@pnp-software.com> wrote in message
news:dhqs2k$pud$1@news.eclipse.org...
> Thanks a lot. I've figured out a similar example last week and it worked
> well with Excel 2003. Has anyody ever thought about programming a code
> generator that would read an idl file and produce similar code as in the
> example given by Toshihiro. Should be easy enough..
> Anybody knows if there's a free parser for idl?
>
> Iwan
>
> Toshihiro Izumi wrote:
>> Here is a SAMPLE for handling Excel Application events.
>>
>> ExcelAppEvents.java
>> --------
>> package org.eclipse.swt.ole.win32;
>>
>> import org.eclipse.swt.internal.ole.win32.COM;
>> import org.eclipse.swt.internal.ole.win32.COMObject;
>> import org.eclipse.swt.internal.ole.win32.GUID;
>> import org.eclipse.swt.internal.ole.win32.IDispatch;
>>
>> public class ExcelAppEvents {
>>
>> public final static GUID IID_AppEvents =
>> COMObject.IIDFromString("{00024413-0000-0000-C000-000000000046} ");
>> // Event ID
>> public final static int NewWorkbook = 0x0000061d;
>> public final static int SheetSelectionChange = 0x00000616;
>> public final static int SheetBeforeDoubleClick = 0x00000617;
>> public final static int SheetBeforeRightClick = 0x00000618;
>> public final static int SheetActivate = 0x00000619;
>> public final static int SheetDeactivate = 0x0000061a;
>> public final static int SheetCalculate = 0x0000061b;
>> public final static int SheetChange = 0x0000061c;
>> public final static int WorkbookOpen = 0x0000061f;
>> public final static int WorkbookActivate = 0x00000620;
>> public final static int WorkbookDeactivate = 0x00000621;
>> public final static int WorkbookBeforeClose = 0x00000622;
>> public final static int WorkbookBeforeSave = 0x00000623;
>> public final static int WorkbookBeforePrint = 0x00000624;
>> public final static int WorkbookNewSheet = 0x00000625;
>> public final static int WorkbookAddinInstall = 0x00000626;
>> public final static int WorkbookAddinUninstall = 0x00000627;
>> public final static int WindowResize = 0x00000612;
>> public final static int WindowActivate = 0x00000614;
>> public final static int WindowDeactivate = 0x00000615;
>> public final static int SheetFollowHyperlink = 0x0000073e;
>>
>> private static IDispatch getApplication(OleControlSite site) {
>> IDispatch application = null;
>> String progID = site.getProgramID();
>> if (progID.startsWith("Excel.Sheet.")) {
>> OleAutomation excelSheet = new OleAutomation(site);
>> int[] dispIDs = excelSheet.getIDsOfNames(new String[]
>> {"Application"});
>> Variant pVarResult = excelSheet.getProperty(dispIDs[0]);
>> if (pVarResult != null && pVarResult.getType() !=
>> COM.VT_EMPTY) {
>> application = pVarResult.getDispatch();
>> }
>> excelSheet.dispose();
>> excelSheet = null;
>> } else { // I don't care.
>> }
>> return application;
>> }
>>
>> public static void addEventListener(OleControlSite site, int eventID,
>> OleListener listener) {
>> IDispatch excelApp = getApplication(site);
>> if (excelApp != null) {
>> site.addEventListener(excelApp.getAddress(), IID_AppEvents,
>> eventID, listener);
>> }
>> }
>>
>> public static void removeEventListener(OleControlSite site, int
>> eventID, OleListener listener) {
>> IDispatch excelApp = getApplication(site);
>> if (excelApp != null) {
>> site.removeEventListener(excelApp.getAddress(), IID_AppEvents,
>> eventID, listener);
>> }
>> }
>>
>> }
>> --------
>>
>> Excel.java
>> --------
>> package test;
>>
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.ole.win32.ExcelAppEvents;
>> import org.eclipse.swt.ole.win32.OLE;
>> import org.eclipse.swt.ole.win32.OleControlSite;
>> import org.eclipse.swt.ole.win32.OleEvent;
>> import org.eclipse.swt.ole.win32.OleFrame;
>> import org.eclipse.swt.ole.win32.OleListener;
>> import org.eclipse.swt.ole.win32.Variant;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Shell;
>>
>> public class Excel {
>>
>> public static void main(String[] args) {
>> Display display = new Display();
>> Shell shell = new Shell(display);
>> OleFrame frame = new OleFrame(shell, SWT.NONE);
>> OleControlSite site = new OleControlSite(frame, SWT.NONE,
>> "Excel.Sheet");
>> site.doVerb(OLE.OLEIVERB_OPEN);
>>
>> // Event WorkbookBeforeClose(Wb As Workbook, Cancel As Boolean)
>> ExcelAppEvents.addEventListener(site,
>> ExcelAppEvents.WorkbookBeforeClose, new OleListener() {
>> public void handleEvent(OleEvent event) {
>> System.out.println("WorkbookBeforeClose");
>> Variant varWorkbook = event.arguments[1];
>> //Variant varCancel = event.arguments[0];
>> varWorkbook.dispose(); //Object must be disposed
>> }
>> });
>> // Event WorkbookBeforeSave(Wb As Workbook, SaveAsUI As Boolean,
>> Cancel As Boolean)
>> ExcelAppEvents.addEventListener(site,
>> ExcelAppEvents.WorkbookBeforeSave, new OleListener() {
>> public void handleEvent(OleEvent event) {
>> System.out.println("WorkbookBeforeSave");
>> Variant varWorkbook = event.arguments[2];
>> //Variant varSaveAsUI = event.arguments[1];
>> Variant varCancel = event.arguments[0];
>> varCancel.setByRef(true); //Cancel for example
>> varWorkbook.dispose(); //Object must be disposed
>> }
>> });
>>
>> shell.open();
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch()) {
>> display.sleep();
>> }
>> }
>> display.dispose();
>> }
>>
>> }
>> --------
>>
>> (tested with Excel2000 only)
>>
Previous Topic:How to disable a MessageDialog
Next Topic:Check Style on Pocket PC
Goto Forum:
  


Current Time: Fri Mar 29 15:40:36 GMT 2024

Powered by FUDForum. Page generated in 0.05140 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top