Samples‎ > ‎MS Office‎ > ‎

Starting Word and handling its events

import com.jniwrapper.Int32;
import com.jniwrapper.win32.automation.OleMessageLoop;
import com.jniwrapper.win32.automation.impl.IDispatchImpl;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.automation.types.VariantBool;
import com.jniwrapper.win32.com.ComFunctions;
import com.jniwrapper.win32.com.server.CoClassMetaInfo;
import com.jniwrapper.win32.com.server.IClassFactoryServer;
import com.jniwrapper.win32.com.types.ClsCtx;
import com.jniwrapper.win32.com.types.IID;
import com.jniwrapper.win32.ole.IConnectionPoint;
import com.jniwrapper.win32.ole.IConnectionPointContainer;
import com.jniwrapper.win32.ole.impl.IConnectionPointContainerImpl;
import word.word.*;
import word.word.server.ApplicationEvents2Server;

import java.io.IOException;

public class WordEventsHandling {

    private OleMessageLoop boundMessageLoop;
    private _Application application;
    private IClassFactoryServer classFactoryServer;
    private Int32 eventsHandlerIdentifier;

    public WordEventsHandling(String fileName) {
        initWordApplication();
        System.out.println(fileName);
        openDocument(fileName);
    }

    private void openDocument(final String fileName) {
        System.out.println("openWorkbook: " + fileName);

        try {
            boundMessageLoop.doInvokeAndWait(new Runnable() {
                public void run() {
                    Documents documents = application.getDocuments();

                    Variant missing = Variant.createUnspecifiedParameter();

                    _Document document = documents.open(new Variant(fileName),
                            missing, missing, missing, missing, missing,
                            missing, missing, missing, missing, missing,
                            missing, missing, missing, missing, missing);
                    document.setAutoDelete(false);
                    document.release();
                    document.setAutoDelete(false);
                    document.release();

                }
            });

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void initWordApplication() {
        ComFunctions.coInitialize();
        boundMessageLoop = new OleMessageLoop(false);
        boundMessageLoop.doStart();
        try {
            boundMessageLoop.doInvokeAndWait(new Runnable() {
                public void run() {
                    application = Application.create(ClsCtx.LOCAL_SERVER);
                    installEventsHandler();
                }
            });
            application = (_Application) boundMessageLoop
                    .bindObject(application);
            application.setVisible( new VariantBool(true));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void installEventsHandler() {
        System.out.println("WordEventsHandling.installEventsHandler");
        IConnectionPointContainer connectionPointContainer = new IConnectionPointContainerImpl(
                application);
        final IID riid = new IID(ApplicationEvents2.INTERFACE_IDENTIFIER);
        IConnectionPoint connectionPoint = null;
        try {
            connectionPoint = connectionPointContainer
                    .findConnectionPoint(riid);
        } finally {
            connectionPointContainer.setAutoDelete(false);
            connectionPointContainer.release();
        }
        classFactoryServer = new IClassFactoryServer(AppEventsHandler.class);
        IDispatchImpl _handler = new IDispatchImpl();
        classFactoryServer.createInstance(null, _handler.getIID(), _handler);

        eventsHandlerIdentifier = connectionPoint.advise(_handler);
        AppEventsHandler eventsHandler = (AppEventsHandler) classFactoryServer
                .getInstances().pop();
        eventsHandler.init(this);
    }

    private void shutdown() throws NoSuchMethodException,
            IOException {
        try {
            boundMessageLoop.doInvokeAndWait(new Runnable() {
                public void run() {
                    uninstallEventsHandler();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        application.setAutoDelete(false);
        application.release();
        boundMessageLoop.doStop();

    }

    private void uninstallEventsHandler() {
        IConnectionPointContainer connectionPointContainer = new IConnectionPointContainerImpl(
                application);
        final IID riid = new IID(ApplicationEvents2.INTERFACE_IDENTIFIER);
        IConnectionPoint connectionPoint = null;
        try {
            connectionPoint = connectionPointContainer
                    .findConnectionPoint(riid);
        } finally {
            connectionPointContainer.setAutoDelete(false);
            connectionPointContainer.release();
        }
        connectionPoint.unadvise(eventsHandlerIdentifier);
        connectionPoint.setAutoDelete(false);
        connectionPoint.release();
    }

    public static class AppEventsHandler extends ApplicationEvents2Server {
        private WordEventsHandling instance;

        public AppEventsHandler(CoClassMetaInfo classImpl) {
            super(classImpl);
            registerMethods(ApplicationEvents2.class, false);
            System.out.println("AppEventsHandler.<init>");
        }

        public void init(WordEventsHandling wordEventsHandling) {
            this.instance = wordEventsHandling;

        }

        public void startup() {
            System.out.println("WordEventsHandling$AppEventsHandler.startup");
        }

        public void quit() {
            System.out.println("WordEventsHandling$AppEventsHandler.quit");
            try {
                instance.shutdown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void documentChange() {
            System.out.println("WordEventsHandling$AppEventsHandler.documentChange");
        }

        public void documentOpen(_Document Doc) {
            System.out.println("WordEventsHandling$AppEventsHandler.documentOpen");
        }

        public void documentBeforeClose(_Document Doc, VariantBool Cancel) {
            System.out.println("WordEventsHandling$AppEventsHandler.documentBeforeClose");
        }

        public void documentBeforePrint(_Document Doc, VariantBool Cancel) {
            System.out.println("WordEventsHandling$AppEventsHandler.documentBeforePrint");
        }

        public void documentBeforeSave(_Document Doc, VariantBool SaveAsUI, VariantBool Cancel) {
            System.out.println("WordEventsHandling$AppEventsHandler.documentBeforeSave");
        }

        public void newDocument(_Document Doc) {
            System.out.println("WordEventsHandling$AppEventsHandler.newDocument");
        }

        /*public void windowActivate(_Document Doc, Window Wn) {
            System.out.println("WordEventsHandling$AppEventsHandler.windowActivate");
        }

        public void windowDeactivate(_Document Doc, Window Wn) {
            System.out.println("WordEventsHandling$AppEventsHandler.windowDeactivate");
        }*/

        public void windowSelectionChange(Selection Sel) {
            System.out.println("WordEventsHandling$AppEventsHandler.windowSelectionChange");
        }

        public void windowBeforeRightClick(Selection Sel, VariantBool Cancel) {
            System.out.println("WordEventsHandling$AppEventsHandler.windowBeforeRightClick");
        }

        public void windowBeforeDoubleClick(Selection Sel, VariantBool Cancel) {
            System.out.println("WordEventsHandling$AppEventsHandler.windowBeforeDoubleClick");
        }
    }

    public static void main(String[] args) {
        WordEventsHandling eventsHandling = new WordEventsHandling("C:\\files\\test.docx");
    }
}