Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] Re: Problem in Caturing excel events form SWT

hi Veronika,

I tried to modify your code and run it for excel but it didnt
work.Esentialy what i am trying to do is if some one changes the value
in some excel sheet or presses some user defined buton to run macro ,I
should get a notification in my java program.

Below is my code which is a modfication of your code.Please see if you
can figure out where I went wrong.
---------------

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.ole.win32.*;
import org.eclipse.swt.internal.ole.win32.*;

public class Snippet123 {

public static void main(String[] args) {
	final Display display = new Display();
	Shell shell = new Shell(display);
	shell.setLayout(new FillLayout());
	OleControlSite controlSite;
	try {
		OleFrame frame = new OleFrame(shell, SWT.NONE);
		controlSite = new OleControlSite(frame, SWT.NONE, "Excel.Sheet");
		int result= controlSite.doVerb(OLE.OLEIVERB_OPEN);
		System.out.println("Result\t"+result);
	} catch (SWTError e) {
		System.out.println("Unable to open activeX control");
		return;
	}
	shell.open();
	
	// IWebBrowser
	//final OleAutomation webBrowser = new OleAutomation(controlSite);

	//ExcelSheet
	 final OleAutomation excelSheet=new OleAutomation(controlSite);
	
	 //When a workbook is opened
	 int Open=0x000102aa;
	 
	// When a new document is loaded, access the document object for the new page.
	//int DownloadComplete = 104;
	 controlSite.addEventListener(Open,new OleListener(){
		 public void handleEvent(OleEvent event){
			 //get the active sheet
			 Variant sheet = excelSheet.getProperty(0x00010133);
			 OleAutomation sheetAutomation=sheet.getAutomation();
			 
			 //Request to be notified of events
			 
			 EventDispatch myDispatch= new EventDispatch(EventDispatch.Activate);
			 IDispatch idispatch=new IDispatch(myDispatch.getAddress());
			 Variant dispatch=new Variant(idispatch);
			 sheetAutomation.setProperty(EventDispatch.Activate,dispatch);
			 
			 myDispatch=new EventDispatch(EventDispatch.Change);
			 idispatch = new IDispatch(myDispatch.getAddress());
			 dispatch = new Variant(idispatch);
			 sheetAutomation.setProperty(EventDispatch.Change,dispatch);
			 
			 
			 myDispatch=new EventDispatch(EventDispatch.Deactivate);
			 idispatch = new IDispatch(myDispatch.getAddress());
			 dispatch = new Variant(idispatch);
			 sheetAutomation.setProperty(EventDispatch.Deactivate,dispatch);
			 
			 //Release the Ole automation Object
			 sheetAutomation.dispose();
		 }
	 });
	 
	 
	
	 //set the value in some cell
	 int id = excelSheet.getIDsOfNames(new String[]{"ActiveSheet"})[0];
		System.out.println(id);
	 Variant sheet = excelSheet.getProperty(id);
	 System.out.println("Sheet\t"+sheet);
	 int rangeId = sheet.getAutomation().getIDsOfNames(new String[]{"Range"})[0];
	 System.out.println("RANGE ID ="+rangeId);
		
	 Variant[] arguments_1 = new Variant[1];
	 arguments_1[0]= new Variant("C2");
		
	 Variant range0 = sheet.getAutomation().getProperty( rangeId, arguments_1 );
	 IDispatch rangeDispatch = range0.getDispatch();
	
	 int valueId = range0.getAutomation().getIDsOfNames(new String[]{"value"})[0];
		
	 boolean value = range0.getAutomation().setProperty(valueId,arguments_1);
     System.out.println(value);
				
	
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	
	//disposing the atomation object
	excelSheet.dispose();
	display.dispose();
	
	
}
}


//EventDispatch implements a simple IDispatch interface which will be
called when the event is fired

class EventDispatch{
	private COMObject iDispatch;
	private int refCount =0;
	private int eventID;
	
	final static int  Activate=0x00010130;
	final static int  Deactivate=0x000105fa;
	final static int  Change=0x00010609;
	
	
	EventDispatch(int eventID)
	{
		this.eventID=eventID;
		createCOMInterfaces();
	}
	
	int getAddress()
	{
		return iDispatch.getAddress();
	}
	
	private void createCOMInterfaces(){
		iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 4, 8}){
			public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
			public int method1(int[] args) {return AddRef();}
			public int method2(int[] args) {return Release();}
			// method3 GetTypeInfoCount - not implemented
			// method4 GetTypeInfo - not implemented
			// method5 GetIDsOfNames - not implemented
			public int method6(int[] args) {return Invoke(args[0], args[1],
args[2], args[3], args[4], args[5], args[6], args[7]);}
		};
	}
	
	private void disposeCOMInterfaces() {
		if (iDispatch != null)
			iDispatch.dispose();
		iDispatch = null;
		
	}
	private int AddRef() {
		refCount++;
		return refCount;
	}
	
	private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags,
int pDispParams, int pVarResult, int pExcepInfo, int pArgErr)
	{
		switch(eventID){
		
		case Activate :{System.out.println("Activate");break;}
		case Change :{System.out.println("Change");break;}
		case Deactivate:{System.out.println("Deactivate");break;}
		}
		return COM.S_OK;
	}
	
	private int QueryInterface(int riid, int ppvObject) {
		if (riid == 0 || ppvObject == 0) return COM.E_INVALIDARG;
		GUID guid = new GUID();
		COM.MoveMemory(guid, riid, GUID.sizeof);
	
		if ( COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid,
COM.IIDIDispatch)) {
			COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4);
			AddRef();
			return COM.S_OK;
		}
		COM.MoveMemory(ppvObject, new int[] {0}, 4);
		return COM.E_NOINTERFACE;
	}
	int Release() {
		refCount--;
		if (refCount == 0) {
			disposeCOMInterfaces();
		}
		return refCount;
	}
}


---------------------------------------------------





On 6/15/05, Abhinav Mathur <abhinavlikesu@xxxxxxxxx> wrote:
> hi,
> i am trying to capture excel events like change in sheet or sheet
> activation or clicking of some use defined button in my java code. I
> am unable to do  it using SWT.can any body tell me where i am going
> wrong or some other API to it.Below s my code
> 
> 
> import java.io.File;
> 
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.SWTException;
> import org.eclipse.swt.internal.ole.win32.IDispatch;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.ole.win32.OLE;
> import org.eclipse.swt.ole.win32.OleAutomation;
> import org.eclipse.swt.ole.win32.OleClientSite;
> 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.Menu;
> import org.eclipse.swt.widgets.Shell;
> //with active sheet
> public class EventTry2 {
>        private Shell shell;
> 
>        private static OleAutomation automation;
> 
>        static OleControlSite controlSite;
> 
>        protected static final int Activate = 0x00010130;
> 
>        protected static final int BeforeDoubleClick = 0x00010601;
> 
>        protected static final int BeforRightClick = 0x000105fe;
> 
>        protected static final int Calculate = 0x00010117;
> 
>        protected static final int Change = 0x00010609;
> 
>        protected static final int Deactivate = 0x000105fa;
> 
>        protected static final int FollowHyperlink = 0x000105be;
> 
>        protected static final int SelectionChange = 0x00010607;
> 
>        public void makeVisible()
>        {
>                Variant[] arguments = new Variant[1];
>                arguments[0] = new Variant("true");
> 
>         //Visible---true
>                automation.setProperty(558,arguments);
> 
> 
>                // EnableEvent--true
>                 automation.setProperty(1212, arguments);
> 
> 
>        }
> 
>        public Shell open(Display display)
>        {
>                this.shell = new Shell(display);
>                this.shell.setLayout(new FillLayout());
>                Menu bar = new Menu(this.shell, SWT.BAR);
>                this.shell.setMenuBar(bar);
>                OleFrame frame = new OleFrame(shell, SWT.NONE);
>                File file = new File("C:\\Book1.xls");
>                try {
>                        controlSite = new OleControlSite(frame, SWT.NONE,
>                                        "Excel.Application");
>                        this.shell.layout();
>                        boolean a2 = true;
>                        a2 = (controlSite.doVerb(OLE.OLEIVERB_SHOW | OLE.OLEIVERB_OPEN
>                                        | OLE.OLEIVERB_UIACTIVATE | OLE.OLEIVERB_HIDE
>                                        | OLE.OLEIVERB_PROPERTIES | OLE.OLEIVERB_INPLACEACTIVATE) == OLE.S_OK);
>                        System.out.println("Activated::\t" + a2);
>                } catch (SWTException ex) {
>                        System.out.println(ex.getMessage());
>                        return null;
>                }
>                automation = new OleAutomation(controlSite);
> 
>                // make the application visible
>                makeVisible();
> 
>                System.out.println("Going to create Event listener");
>                OleListener eventListener = new OleListener() {
> 
>                        public void handleEvent(OleEvent event) {
>                                System.out.println("EVENT TYPE==\t" + event.type);
>                                switch (event.type) {
> 
>                                case Activate: {
>                                        System.out.println("Activate Event");
>                                }
> 
>                                case BeforeDoubleClick: {
>                                        System.out.println("BeforeDoubleClick Event");
>                                }
> 
>                                case BeforRightClick: {
>                                        System.out.println("BeforeRightClick Event");
>                                }
> 
>                                case Calculate: {
>                                        System.out.println("Calculate Event");
>                                }
> 
>                                case Change: {
>                                        System.out.println("Change Event");
>                                }
> 
>                                case Deactivate: {
>                                        System.out.println("DeActivate Event");
>                                }
> 
>                                case FollowHyperlink: {
>                                        System.out.println("Activate Event");
>                                }
> 
>                                case SelectionChange: {
>                                        System.out.println("Activate Event");
>                                }
> 
>                                }
>                                Variant[] arguments = event.arguments;
>                                for (int i = 0; i < arguments.length; i++) {
>                                        System.out.println("@@");
>                                        arguments[i].dispose();
>                                }
>                        }
> 
>                };
> 
>                System.out.println("outside");
>                OleAutomation sheetAutomation = this.openFile("C:\\Book1.xls");
> 
>                controlSite.addEventListener(sheetAutomation, Activate, eventListener);
>                controlSite.addEventListener(sheetAutomation, BeforeDoubleClick,
>                                eventListener);
>                controlSite.addEventListener(sheetAutomation, BeforRightClick,
>                                eventListener);
>                controlSite.addEventListener(sheetAutomation, Calculate, eventListener);
>                controlSite.addEventListener(sheetAutomation, Change, eventListener);
>                controlSite
>                                .addEventListener(sheetAutomation, Deactivate, eventListener);
>                controlSite.addEventListener(sheetAutomation, FollowHyperlink,
>                                eventListener);
>                controlSite.addEventListener(sheetAutomation, SelectionChange,
>                                eventListener);
> 
>                int rangeId = sheetAutomation.getIDsOfNames(new String[] { "Range" })[0];
>                System.out.println(rangeId);
>                Variant[] arguments_1 = new Variant[1];
>                arguments_1[0] = new Variant("C2");
>                Variant range0 = sheetAutomation.getProperty(rangeId, arguments_1);
>                IDispatch rangeDispatch = range0.getDispatch();
> 
>                int valueId = range0.getAutomation().getIDsOfNames(
>                                new String[] { "value" })[0];
> 
>                Variant value = range0.getAutomation().getProperty(valueId);
> 
>                System.out.println(value.getInt());
> 
>                Variant[] newValue = new Variant[1];
>                newValue[0] = new Variant(216);
> 
>                range0.getAutomation().setProperty(valueId, newValue);
>                value = range0.getAutomation().getProperty(valueId);
> 
>                System.out.println(value.getInt());
> 
>                shell.open();
>                return shell;
>        }
> 
>        public OleAutomation openFile(String fileName) {
>                Variant workbooks = automation.getProperty(0x0000023c);// get User
>                                                                                                                                // Defined
>                                                                                                                                // Workbooks
>                Variant[] arguments = new Variant[1];
> 
>                arguments[0] = new Variant(fileName);
>                System.out.println("workbooks::\t" + workbooks);
> 
> 
>                int[] rgdispid = workbooks.getAutomation().getIDsOfNames(
>                                new String[] { "Open" });
>                int dispIdMember = rgdispid[0];
>                Variant workbook = workbooks.getAutomation().invoke(dispIdMember,
>                                arguments);
>                System.out.println("Opened the Work Book");
>                try {
>                        Thread.sleep(500);
>                } catch (InterruptedException e) {
>                        // TODO Auto-generated catch block
>                        e.printStackTrace();
>                }
>                int id = workbook.getAutomation().getIDsOfNames(
>                                new String[] { "ActiveSheet" })[0];
>                System.out.println(id);
>                Variant sheet = workbook.getAutomation().getProperty(id);
>                OleAutomation sheetAutomation = sheet.getAutomation();
> 
>                return (sheetAutomation);
>        }
> 
>        /**
>         * @param args
>         */
>        public static void main(String[] args) {
>                // TODO Auto-generated method stub
> 
>                Display display = new Display();
>                Shell shell = (new EventTry2()).open(display);
>                while (!shell.isDisposed()) {
>                        if (!display.readAndDispatch()) {
>                                display.sleep();
>                        }
>                }
>                controlSite.dispose();
>                display.dispose();
> 
>                System.out
>                                .println("-----------------THE END-----------------------------");
> 
>        }
> 
> }
>


Back to the top