Skip to main content

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

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