Search code examples
eclipseswteclipse-rcprcpole

how to get word DocumentBeforeSaveEvent with ole listener?


I'm trying to embed word in my eclipse RCP application. Having problems to save an open editor if CTRL+S is pressed inside of the word application.

I tried the following snipped but even in this (from eclipse separated) snipped I do not get the DocumentBeforeSaveEvent event:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
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 WordMain {

    final static int DocumentBeforeSaveEvent = 0x00000008;

    public static void main(String[] args) {
        Display display = new Display();

        Shell shell = new Shell(display);
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        shell.setLayout(new GridLayout());
        frame.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));

        OleControlSite site = new OleControlSite(frame, SWT.NONE, "Word.Document");
        OleAutomation doc = new OleAutomation(site);
        int[] dispInfo = doc.getIDsOfNames(new String[] { "Application" });
        Variant variant = doc.getProperty(dispInfo[0]);
        OleAutomation app = variant.getAutomation();
        variant.dispose();

        OleListener listener = new OleListener() {

            @Override
            public void handleEvent(OleEvent event) {
                System.out.println("DocumentBeforeSave Event, no. params:" + event.arguments.length);
            }
        };

        site.addEventListener(app, DocumentBeforeSaveEvent, listener);

        site.doVerb(OLE.OLEIVERB_OPEN);
        shell.open();

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

        app.dispose();

    }
}

Snippet source: http://www.eclipse.org/forums/index.php/t/139593/

Maybe DocumentBeforeSaveEvent is the wrong eventId and I need some kind of key Listener, listening to CTRL+S?

Here is a list of all the eventIds, DocumentBeforeSave seems the only one which I could use:

0x2 Quit
0x3 DocumentChange
0x4 DocumentOpen
0x6 DocumentBeforeClose
0x7 DocumentBeforePrint
0x8 DocumentBeforeSave
0x9 NewDocument
0xa WindowActivate
0xb WindowDeactivate
0xc WindowSelectionChange
0xd WindowBeforeRightClick
0xe WindowBeforeDoubleClick
0xf EPostagePropertyDialog
0x10 EPostageInsert
0x11 MailMergeAfterMerge
0x12 MailMergeAfterRecordMerge
0x13 MailMergeBeforeMerge
0x14 MailMergeBeforeRecordMerge
0x15 MailMergeDataSourceLoad
0x16 MailMergeDataSourceValidate
0x17 MailMergeWizardSendToCustom
0x18 MailMergeWizardStateChange
0x19 WindowSize
0x1a XMLSelectionChange
0x1b XMLValidationError
0x1c DocumentSync
0x1d EPostageInsertEx
0x1e MailMergeDataSourceValidate2
0x1f ProtectedViewWindowOpen
0x20 ProtectedViewWindowBeforeEdit
0x21 ProtectedViewWindowBeforeClose
0x22 ProtectedViewWindowSize
0x23 ProtectedViewWindowActivate
0x24 ProtectedViewWindowDeactivate

What I noticed, the embeded word has it's own "save" button (top left) which is always deactivated, even if the word is modified and the should be "dirty". So maybe activating the save button (or save functionality) would then also trigger the DocumentBeforeSaveEvent event.

Using MS Word 2010, Java 7.


Solution

  • This seems to be an eclipse bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=64488

    Looks like it is inactive.