OleControlSite Ms Office als Read Only öffnen / Property setzen

DontBeEvil

Grünschnabel
Moi,

hoffe jemand kennt sich hier mit Ole in Java aus.

Ich Suche einen weg wie ich ein Word Document über Java Read Only setzten kann, solange ich es in meiner Java Anwendung geöffnet habe.

Über die Properties der Automation, kann ich mir von der Application den Wert von ReadOnly auslesen jedoch nicht setzten, da keine set Methode zur verfügung gestellt wird von der Automation :(

Kenn jemand einen weg oder bin ich an der total falschen Property?

Hier ein kleiner beispiel Code wo und wie ich mir die automations hole.
Code:
package OleWord;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.awt.SWT_AWT;
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.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class OleWordExample {
    private OleControlSite controlSite;
    private OleAutomation automation;
    private OleFrame frame;
    private JFrame jframe;
    private Canvas canvas;
    private Display display;
    private Shell shell;
    private Shell swtShell;

    public OleWordExample() {

        // Word word = new Word();

        init();
        jframe.setVisible(true);
        System.setProperty("sun.awt.noerasebackground", "true");

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

    private void init() {
        display = new Display();
        shell = new Shell(display);

        canvas = new Canvas();
        jframe = new JFrame("Ole Microsoft Word");

        final int width = 1378;
        final int height = 768;
        jframe.setSize(600, 600);
        // jframe.getContentPane().add(canvas);
        JPanel x = new JPanel();
        x.setLayout(new BorderLayout());
        x.setPreferredSize(new Dimension(600, 600));
        x.add(canvas);
        // x.setVisible(true);
        jframe.add(x);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // jframe.setExtendedState(JFrame.MAXIMIZED_BOTH);

        display.asyncExec(new Runnable() {
            public void run() {
                swtShell = SWT_AWT.new_Shell(display, canvas);

                swtShell.setSize(1024, 768);
                swtShell.setLayout(new FillLayout());

                try {

                    frame = new OleFrame(swtShell, SWT.NONE);
                    controlSite = new OleControlSite(frame, SWT.NULL,
                            "Word.Document");

                    controlSite.doVerb(OLE.OLEIVERB_OPEN);
                    OleAutomation automation = new OleAutomation(controlSite);

                    int[] dispIDs = automation
                            .getIDsOfNames(new String[] { "Application" });
                    Variant pVarResult = automation.getProperty(dispIDs[0]);
                    OleAutomation application = pVarResult.getAutomation();

                    int[] appID = application
                            .getIDsOfNames(new String[] { "Documents" });
                    Variant appresult = application.getProperty(appID[0]);
                    OleAutomation documents = appresult.getAutomation();

                } catch (final SWTError e) {
                    System.out.println("Unable to open activeX control");
                    display.dispose();
                    return;
                }
                swtShell.open();

            }
        });

    }

    public static void main(final String[] args) {
        final OleWordExample example = new OleWordExample();
    }

}
 
Hallo,

wenn du dein Word-Dokument vor dem Öffnen als File-Objekt hast, kannst du ja die Methoden File#setReadOnly() und danach File#setWritable() aufrufen.

Grüße
 
Hi,
vielen Dank für die Antwort.

Aber ich muss nicht das File schützen, sondern die Eingabemöglichkeiten bzw. das Editieren des Dokuments im Word unterbinden.

Habe auch schon die controlsite disabeld. Jedoch funktionieren dann die Scrollbars auch nicht mehr und das Navigieren innerhalb des Dokuments ist nicht mehr möglich.
 
Hallo,

ist die Word zuverlässig? Ich habe das problem dass ich manchmal null Werte zurückkriege und manchmal funktioneirt alles prima.
Keine Ahnung warum das so ist. Irgendwelche Ideen?
 
Zurück