[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Excel event handling solution

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)