com.jniwrapper.win32.automation.AutomationException: COM object method returns error code: 0x8002000

99 views
Skip to first unread message

riazkha...@gmail.com

unread,
Mar 31, 2018, 4:28:20 AM3/31/18
to JExcel Forum
com.jniwrapper.win32.automation.AutomationException: COM object method returns error code: 0x80020009; DISP_E_EXCEPTION (Exception occurred.)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:763)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:742)
at com.jniwrapper.win32.automation.impl.IDispatchImpl.invoke(SourceFile:112)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1104)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1095)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.setProperty(SourceFile:1052)
at com.jniwrapper.win32.automation.Automation.a(SourceFile:508)
at com.jniwrapper.win32.automation.Automation.setProperty(SourceFile:582)
at com.jniwrapper.win32.automation.Automation.setDispatchProperty(SourceFile:652)
at com.jniwrapper.win32.excel.impl.RangeImpl.setLocked(Unknown Source)
at jxcell.ProtectedWorkbookSample$3.run(ProtectedWorkbookSample.java:96)
at com.jniwrapper.win32.MessageLoopThread$ThreadAction.run(MessageLoopThread.java:601)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:575)
com.jniwrapper.win32.automation.AutomationException: COM object method returns error code: 0x80020009; DISP_E_EXCEPTION (Exception occurred.)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:763)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:742)
at com.jniwrapper.win32.automation.impl.IDispatchImpl.invoke(SourceFile:112)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1104)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1095)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.setProperty(SourceFile:1052)
at com.jniwrapper.win32.automation.Automation.a(SourceFile:508)
at com.jniwrapper.win32.automation.Automation.setProperty(SourceFile:582)
at com.jniwrapper.win32.automation.Automation.setDispatchProperty(SourceFile:652)
at com.jniwrapper.win32.excel.impl.RangeImpl.setLocked(Unknown Source)
at jxcell.ProtectedWorkbookSample$3.run(ProtectedWorkbookSample.java:96)
at com.jniwrapper.win32.MessageLoopThread$ThreadAction.run(MessageLoopThread.java:601)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:575)
com.jniwrapper.win32.automation.AutomationException: COM object method returns error code: 0x80020009; DISP_E_EXCEPTION (Exception occurred.)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:763)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:742)
at com.jniwrapper.win32.automation.impl.IDispatchImpl.invoke(SourceFile:112)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1104)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.a(SourceFile:1095)
at com.jniwrapper.win32.automation.Automation$InvocationHelper.setProperty(SourceFile:1052)
at com.jniwrapper.win32.automation.Automation.a(SourceFile:508)
at com.jniwrapper.win32.automation.Automation.setProperty(SourceFile:582)
at com.jniwrapper.win32.automation.Automation.setDispatchProperty(SourceFile:652)
at com.jniwrapper.win32.excel.impl.RangeImpl.setLocked(Unknown Source)
at jxcell.ProtectedWorkbookSample$3.run(ProtectedWorkbookSample.java:96)
at com.jniwrapper.win32.MessageLoopThread$ThreadAction.run(MessageLoopThread.java:601)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:575)
194496 [JExcel.JWorkbook message loop #1522482252732] ERROR com.jniwrapper.win32.jexcel.Application - Failed to restore IgnoreRemoteRequests() state
com.jniwrapper.win32.com.ComException: COM object method returns error code: 0x80010105; The server threw an exception.
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:763)
at com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:742)
at com.jniwrapper.win32.excel.impl._ApplicationImpl.setIgnoreRemoteRequests(Unknown Source)
at com.jniwrapper.win32.jexcel.fk.run(Unknown Source)
at com.jniwrapper.win32.MessageLoopThread$ThreadSynchronizedAction.run(MessageLoopThread.java:620)
at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:562)

Anna Dolbina

unread,
Apr 2, 2018, 5:36:36 AM4/2/18
to riazkha...@gmail.com, JExcel Forum
Hi,

Is it possible for you to attach the source code and the sample Excel workbook that you used to reproduce these exceptions?

Sincerely,
Anna



--
You received this message because you are subscribed to the Google Groups "JExcel Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jexcel-forum+unsubscribe@teamdev.com.
To view this discussion on the web visit https://groups.google.com/a/teamdev.com/d/msgid/jexcel-forum/8364f726-eeb1-40a6-8155-6311c107cd2f%40teamdev.com.

riazkha...@gmail.com

unread,
Apr 7, 2018, 3:46:06 AM4/7/18
to JExcel Forum, riazkha...@gmail.com
Hi,

My code is

 package jxcell;

import com.jniwrapper.Int32;
import com.jniwrapper.win32.automation.impl.IDispatchImpl;
import com.jniwrapper.win32.automation.types.BStr;
import com.jniwrapper.win32.automation.types.Variant;
import com.jniwrapper.win32.automation.types.VariantBool;
import com.jniwrapper.win32.com.ComException;
import com.jniwrapper.win32.com.server.CoClassMetaInfo;
import com.jniwrapper.win32.com.server.IClassFactoryServer;
import com.jniwrapper.win32.com.types.IID;
import com.jniwrapper.win32.excel.AppEvents;
import com.jniwrapper.win32.excel._Workbook;
import com.jniwrapper.win32.excel._Worksheet;
import com.jniwrapper.win32.excel.server.AppEventsServer;
import com.jniwrapper.win32.jexcel.*;
import com.jniwrapper.win32.jexcel.ui.JWorkbook;
import com.jniwrapper.win32.ole.IConnectionPoint;
import com.jniwrapper.win32.ole.IConnectionPointContainer;
import com.jniwrapper.win32.ole.impl.IConnectionPointContainerImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.lang.reflect.InvocationTargetException;

/**
 * This sample demonstrates how to open a workbook and disable the following
 * actions:
 *
 * - Cut, copy, paste shortcuts - Printing - Saving - Right-clicking cells -
 * Editing cells
 *
 */
public class ProtectedWorkbookSample extends JFrame {

    public static final String PASSWORD = "password";
    private final Container contentPane;
    private JWorkbook uiWorkbook;
    private PrintingHandler printingHandler;

    public ProtectedWorkbookSample(File file) {
        super("Protected JWorkbook Window");
        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        //Create JWorkbook
        createJWorkbook(file);
        protectWorksheets();
        //Add window state listener to clean up after yourself
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                closeJWorkbook();
            }
        });
    }

    public static void main(String[] args) {
        ProtectedWorkbookSample sampleWindow = new ProtectedWorkbookSample(new File("F:\\Work\\Nadeem bhai\\files\\template.xlsx"));
        sampleWindow.setSize(800, 600);
        sampleWindow.setLocationRelativeTo(null);
        sampleWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        sampleWindow.setVisible(true);
    }

    private static void unprotectWorksheet(final Worksheet worksheet) {
        try {
            worksheet.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                public void run() {
                    try {
                        _Worksheet _worksheet = worksheet.getPeer();
                        _worksheet.unprotect(
                                new Variant(PASSWORD),
                                new Int32(0)
                        );
                        _worksheet.getCells().setLocked(new Variant(false));//unprotect all cells on the worksheet
                    } catch (ComException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void protectWorksheet(final Worksheet worksheet) {
        try {
            worksheet.getOleMessageLoop().doInvokeLater(new Runnable() {
                public void run() {
                    try {
                        _Worksheet _worksheet = worksheet.getPeer();
                        _worksheet.getCells().setLocked(new Variant(true));//protect all cells on the worksheet
                        Variant missing = Variant.createUnspecifiedParameter();
                        _worksheet.protect(
                                new Variant(PASSWORD),
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing,
                                missing
                        );
                    } catch (ComException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void closeJWorkbook() {
        if (printingHandler != null) {
            printingHandler.release();
        }
        unprotectWorksheets();
        uiWorkbook.close();
    }

    private void createJWorkbook(File file) {
        try {
            if (file == null) {
                uiWorkbook = new JWorkbook();
            } else {
                uiWorkbook = new JWorkbook(file);
            }
        } catch (Exception e) {
            throw new RuntimeException("Unable to create JWorkbook", e);
        }

        //Insert the JWorkbook into JFrame
        contentPane.add(uiWorkbook, BorderLayout.CENTER);
        setupEventHandlers();
        setEnableCutCopyPasteShortcuts(uiWorkbook.getApplication(), false);
        protectWorksheets();
    }

    private void setupEventHandlers() {
        WorkbookEventHandler workbookEventHandler = new WorkbookHandler();
        WorksheetEventHandler worksheetEventHandler = new WorksheetHandler();

        uiWorkbook.setEventHandler(workbookEventHandler);

        java.util.List worksheets = uiWorkbook.getWorksheets();
        for (int i = 0; i < worksheets.size(); i++) {
            Worksheet worksheet = (Worksheet) worksheets.get(i);
            worksheet.setEventHandler(worksheetEventHandler);
        }

        printingHandler = new PrintingHandler();
        printingHandler.attach(uiWorkbook.getApplication());
    }

    private void protectWorksheets() {
        java.util.List worksheets = uiWorkbook.getWorksheets();
        for (int i = 0; i < worksheets.size(); i++) {
            Worksheet worksheet = (Worksheet) worksheets.get(i);
            protectWorksheet(worksheet);
        }
    }

    private void unprotectWorksheets() {
        java.util.List worksheets = uiWorkbook.getWorksheets();
        for (int i = 0; i < worksheets.size(); i++) {
            Worksheet worksheet = (Worksheet) worksheets.get(i);
            unprotectWorksheet(worksheet);
        }
    }

    private void setEnableCutCopyPasteShortcuts(final Application application, boolean value) {
        final Variant action = value ? Variant.createUnspecifiedParameter() : new Variant("");
        try {
            application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                public void run() {
                    application.getPeer().onKey(new BStr("^c"), action, new Int32(0));
                    application.getPeer().onKey(new BStr("^v"), action, new Int32(0));
                    application.getPeer().onKey(new BStr("^x"), action, new Int32(0));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class WorkbookHandler implements WorkbookEventHandler {

        public boolean beforeClose(WorkbookEventObject source) {
            return true;
        }

        public boolean beforeSave(WorkbookEventObject source) {
            return false;
        }
    }

    public static class WorksheetHandler implements WorksheetEventHandler {

        public boolean beforeDoubleClick(WorksheetEventObject eventObject) {
            return true;
        }

        public boolean beforeRightClick(WorksheetEventObject eventObject) {
            return false;
        }
    }

    public static class PrintingHandler {

        private IConnectionPoint _connectionPoint;
        private IClassFactoryServer classFactoryServer;
        private IDispatchImpl _handler;
        private Int32 _connectionPointID;
        private AppEventsHandler _eventHandler;
        private Application application;

        public void attach(final Application application) {
            this.application = application;
            try {
                application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        IConnectionPointContainer connectionPointContainer = new IConnectionPointContainerImpl(application.getPeer());
                        final IID riid = new IID(AppEvents.INTERFACE_IDENTIFIER);
                        try {
                            _connectionPoint = connectionPointContainer.findConnectionPoint(riid);
                        } finally {
                            connectionPointContainer.setAutoDelete(false);
                            connectionPointContainer.release();
                        }
                        classFactoryServer = new IClassFactoryServer(AppEventsHandler.class);
                        _handler = new IDispatchImpl();
                        classFactoryServer.createInstance(null, _handler.getIID(), _handler);

                        _connectionPointID = _connectionPoint.advise(_handler);
                        _eventHandler = (AppEventsHandler) classFactoryServer.getInstances().pop();
                    }

                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        public void release() {
            try {
                application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                    @Override
                    public void run() {
                        if (_connectionPoint != null && !_connectionPoint.isNull()) {
                            _connectionPoint.unadvise(_connectionPointID);
                            _connectionPoint.setAutoDelete(false);
                            _connectionPoint.release();
                            _connectionPoint = null;
                        }

                        if (_handler != null && !_handler.isNull()) {
                            _handler.setAutoDelete(false);
                            _handler.release();
                            _handler = null;

                            _eventHandler.setAutoDelete(false);
                            _eventHandler = null;
                        }
                        // classFactoryServer should be released last one!
                        if (classFactoryServer != null && !classFactoryServer.isNull()) {
                            classFactoryServer.setAutoDelete(false);
                            classFactoryServer.release();
                            classFactoryServer = null;
                        }
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        public static class AppEventsHandler extends AppEventsServer {

            public AppEventsHandler(CoClassMetaInfo classImpl) {
                super(classImpl);
            }

            public void workbookBeforePrint(_Workbook Wb, VariantBool Cancel) {
                Cancel.setBooleanValue(true);
            }
        }
    }
}

and the file is also attach

Sincerely,
Anna



To unsubscribe from this group and stop receiving emails from it, send an email to jexcel-forum...@teamdev.com.
template.xlsx
Reply all
Reply to author
Forward
0 new messages