com.jacob.com.Dispatch.invoke()

Here are the examples of the java api com.jacob.com.Dispatch.invoke() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

3 Examples 7

16 Source : Access.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * gets the columns form the rec set
 *
 * @param recset
 * @return list of column names
 */
public static String[] getColumns(Dispatch recset) {
    Dispatch flds = Dispatch.get(recset, "Fields").toDispatch();
    int n_flds = Dispatch.get(flds, "Count").getInt();
    String[] s = new String[n_flds];
    Variant vi = new Variant();
    for (int i = 0; i < n_flds; i++) {
        vi.putInt(i);
        // must use the invoke method because this is a method call
        // that wants to have a Dispatch.Get flag...
        Dispatch fld = Dispatch.invoke(recset, "Fields", Dispatch.Get, new Object[] { vi }, new int[1]).toDispatch();
        Variant name = Dispatch.get(fld, "Name");
        s[i] = name.toString();
    }
    return s;
}

13 Source : ExcelEventTest.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * load up excel, register for events and make stuff happen
 *
 * @param args
 */
public void testExcelWithInvocationProxy() {
    ComThread.InitSTA();
    // we are going to listen to events on Application.
    // You can probably also listen Excel.Sheet and Excel.Chart
    String excelApplicationProgramId = "Excel.Application";
    String excelSheetProgramId = "Excel.Sheet";
    String typeLibLocation;
    // office 2003
    typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE";
    // office 2007
    typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE12\\EXCEL.EXE";
    // office 2013 32 bit
    typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office14\\EXCEL.EXE";
    // Office 2013 32
    typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE";
    // Office 2019 32
    typeLibLocation = "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE";
    // Grab The Component.
    ActiveXComponent axc = new ActiveXComponent(excelApplicationProgramId);
    hookupListener(axc, excelApplicationProgramId, typeLibLocation);
    try {
        System.out.println("version=" + axc.getProperty("Version"));
        System.out.println("version=" + Dispatch.get(axc, "Version"));
        axc.setProperty("Visible", true);
        Dispatch workbooks = axc.getPropertyAsComponent("Workbooks");
        Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
        Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
        System.out.println("Workbook: " + workbook);
        System.out.println("Sheet: " + sheet);
        if (typeLibLocation.contains("OFFICE11")) {
            // office 2007 throws crashes the VM
            System.out.println("Hooking up sheet listener");
            hookupListener(sheet, excelSheetProgramId, typeLibLocation);
        }
        System.out.println("Retrieving cells");
        Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1" }, new int[1]).toDispatch();
        Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A2" }, new int[1]).toDispatch();
        System.out.println("Inserting value into A1");
        System.out.println("Inserting calculation 2xA1 into A2");
        Dispatch.put(a1, "Value", "123.456");
        Dispatch.put(a2, "Formula", "=A1*2");
        System.out.println("Retrieved a1 from excel:" + Dispatch.get(a1, "Value"));
        System.out.println("Retrieved a2 from excel:" + Dispatch.get(a2, "Value"));
        Variant f = new Variant(false);
        Dispatch.call(workbook, "Close", f);
        axc.invoke("Quit", new Variant[] {});
    } catch (ComException cfe) {
        cfe.printStackTrace();
        fail("Failed to attach to " + excelApplicationProgramId + ": " + cfe.getMessage());
    }
    try {
        // the sleep is required to let everything clear out after the quit
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ComThread.Release();
}

13 Source : ExcelDispatchTest.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * main run loop for test program
 *
 * @param args
 *            standard command line arguments
 */
public static void main(String[] args) {
    ComThread.InitSTA();
    ActiveXComponent xl = new ActiveXComponent("Excel.Application");
    try {
        System.out.println("version=" + xl.getProperty("Version"));
        System.out.println("version=" + Dispatch.get(xl, "Version"));
        Dispatch.put(xl, "Visible", new Variant(true));
        Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
        Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
        Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
        Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A1" }, new int[1]).toDispatch();
        Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, new Object[] { "A2" }, new int[1]).toDispatch();
        Dispatch.put(a1, "Value", "123.456");
        Dispatch.put(a2, "Formula", "=A1*2");
        System.out.println("a1 from excel:" + Dispatch.get(a1, "Value"));
        System.out.println("a2 from excel:" + Dispatch.get(a2, "Value"));
        Variant f = new Variant(false);
        Dispatch.call(workbook, "Close", f);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        xl.invoke("Quit", new Variant[] {});
        ComThread.Release();
    }
}