Samples‎ > ‎NativePeer‎ > ‎

Connect and Disconnect Excel Add-ins


The following sample code demonstrates how to get the list of COM Add-ins in JExcel and disconnect them:

                final Application application = jWorkbook.getApplication();
                try {
                    application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                        public void run() {
                            COMAddIns comAddIns = application.getPeer().getCOMAddIns();
                            long count = comAddIns.getCount().getValue();
                            for (int i = 1; i <= count; i++) {
                                COMAddIn comAddIn = comAddIns.item(new Variant(i));
                                BStr description = comAddIn.getDescription();
                                VariantBool connected = comAddIn.getConnect();
                                System.out.println("COM add-in: " + description.getValue() + ", connected = "+connected);
                                try {
                                    if (connected.getBooleanValue()) {
                                        comAddIn.setConnect(VariantBool.FALSE);
                                    }
                                } catch (ComException e1) {
                                    //Setting Connect to false may fail if add-in is installed for all users 
                                    //and can only be connected or disconnected by an administrator.
                                    e1.printStackTrace();
                                }
                            }

                        }
                    });
                } catch (Exception e1) {
                    e1.printStackTrace();
To connect the COM add-in back, you need to set its Connect property to true.

The regular Excel add-ins can be disconnected in a similar way. The application.getPeer().getAddIns() call will return the add-ins collection, and the AddIn.Installed property can be used to enable or disable that add-in:

        try {
            application.getOleMessageLoop().doInvokeAndWait(new Runnable() {
                public void run() {
                    AddIns addIns = application.getPeer().getAddIns();
                    long count = addIns.getCount().getValue();
                    for (int i = 1; i <= count; i++) {
                        AddIn addIn = addIns.getItem(new Variant(i));
                        BStr description = addIn.getFullName();
                        VariantBool connected = addIn.getInstalled();
                        System.out.println("COM add-in: " + description.getValue() + ", connected = "+connected);
                        try {
                            if (connected.getBooleanValue()) {
                                addIn.setInstalled(VariantBool.FALSE);
                            }
                        } catch (ComException e1) {
                            e1.printStackTrace();
                        }
                    }

                }
            });
        } catch (Exception e1) {
            e1.printStackTrace();
        }