Search in sources :

Example 6 with LCID

use of com.sun.jna.platform.win32.WinDef.LCID in project jna by java-native-access.

the class Excelautomation_KB_219151_Mod method main.

public static void main(String[] args) throws IOException {
    // Initialize COM Subsystem
    Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    // Initialize Factory for COM object creation
    Factory fact = new Factory();
    // Set LCID for calls to english locale. Without this formulas need
    // to be specified in the users locale.
    fact.setLCID(new LCID(0x0409));
    try {
        // Start excel application
        ComExcel_Application excel = fact.createObject(ComExcel_Application.class);
        ComIApplication excelApp = excel.queryInterface(ComIApplication.class);
        // Set visiblite of application
        excelApp.setVisible(true);
        Helper.sleep(5);
        // Get a new workbook.
        ComIWorkbook wb = excelApp.getWorkbooks().Add();
        ComIWorksheet sheet = wb.getActiveSheet();
        // Add table headers going cell by cell.
        sheet.getCells().getItem(1, 1).setValue("First Name");
        sheet.getCells().getItem(1, 2).setValue("Last Name");
        sheet.getCells().getItem(1, 3).setValue("Full Name");
        sheet.getCells().getItem(1, 4).setValue("Salary");
        // Create an array to set multiple values at once.
        SAFEARRAY saNames = safeVariantArrayFromJava(new String[][] { { "John", "Smith" }, { "Tom", "Brown" }, { "Sue", "Thomas" }, { "Jane", "Jones" }, { "Adam", "Johnson" } });
        // Fill A2:B6 with an array of values (First and Last Names).
        VARIANT valueHolder = new VARIANT();
        valueHolder.setValue(Variant.VT_ARRAY | Variant.VT_VARIANT, saNames);
        sheet.getRange("A2", "B6").setValue(valueHolder);
        saNames.destroy();
        // Fill C2:C6 with a relative formula (=A2 & " " & B2).
        sheet.getRange("C2", "C6").setFormula("= A2 & \" \" & B2");
        // Fill D2:D6 with a formula(=RAND()*100000) and apply format.
        sheet.getRange("D2", "D6").setFormula("=RAND()*100000");
        sheet.getRange("D2", "D6").setNumberFormat("$0.00");
        // AutoFit columns A:D.
        sheet.getRange("A1", "D2").getEntireColumn().AutoFit();
        displayQuaterlySales(sheet);
        File tempFile = Helper.createNotExistingFile("exceloutput", ".xlsx");
        System.out.println("Writing output to: " + tempFile.getAbsolutePath());
        wb.SaveAs(tempFile.getAbsolutePath());
        excelApp.setUserControl(true);
    } finally {
        fact.disposeAll();
        Ole32.INSTANCE.CoUninitialize();
    }
    System.exit(0);
}
Also used : LCID(com.sun.jna.platform.win32.WinDef.LCID) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) ComIWorkbook(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook) Factory(com.sun.jna.platform.win32.COM.util.Factory) ComExcel_Application(com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication) ComIWorksheet(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)

Example 7 with LCID

use of com.sun.jna.platform.win32.WinDef.LCID in project jna by java-native-access.

the class ComEventCallbacks_Test method testComEventCallback.

@Test
public void testComEventCallback() throws InterruptedException {
    VARIANT.ByReference pVarResult = new VARIANT.ByReference();
    IntByReference puArgErr = new IntByReference();
    EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    HRESULT hr;
    DISPPARAMS.ByReference pDispParams;
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(new VARIANT[] { new VARIANT(true) });
    pDispParams.setRgdispidNamedArgs(new DISPID[] { OaIdl.DISPID_PROPERTYPUT });
    // Visible-Prioperty
    hr = ieDispatch.Invoke(dispIdVisible.getValue(), niid, lcid, propertyPutFlags, pDispParams, null, null, null);
    COMUtils.checkRC(hr);
    // query for ConnectionPointContainer
    Unknown unk = new Unknown(ieApp.getValue());
    PointerByReference ppCpc = new PointerByReference();
    hr = unk.QueryInterface(new REFIID(IID_IConnectionPointContainer), ppCpc);
    COMUtils.checkRC(hr);
    ConnectionPointContainer cpc = new ConnectionPointContainer(ppCpc.getValue());
    // find connection point for DWebBrowserEvents2
    REFIID riid = new REFIID(IID_DWebBrowserEvents2);
    PointerByReference ppCp = new PointerByReference();
    hr = cpc.FindConnectionPoint(riid, ppCp);
    COMUtils.checkRC(hr);
    final ConnectionPoint cp = new ConnectionPoint(ppCp.getValue());
    IID cp_iid = new IID();
    hr = cp.GetConnectionInterface(cp_iid);
    COMUtils.checkRC(hr);
    final DWebBrowserEvents2_Listener listener = new DWebBrowserEvents2_Listener();
    final DWORDByReference pdwCookie = new DWORDByReference();
    HRESULT hr1 = cp.Advise(listener, pdwCookie);
    COMUtils.checkRC(hr1);
    // Advise make several callbacks into the object passed in - at this
    // point QueryInterface must have be called multiple times
    Assert.assertTrue(listener.QueryInterface_called);
    // Call Navigate with URL https://github.com/java-native-access/jna
    String navigateURL = "https://github.com/java-native-access/jna";
    String blockedURL = "http://www.google.de";
    VARIANT[] arguments = new VARIANT[] { new VARIANT(navigateURL) };
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(arguments);
    hr = ieDispatch.Invoke(dispIdNavigate.getValue(), niid, lcid, methodFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    for (int i = 0; i < 10; i++) {
        if (listener.navigateComplete2Called) {
            break;
        }
        Thread.sleep(1000);
    }
    // At this point the call to Navigate before should be complete
    Assert.assertTrue(listener.navigateComplete2Called);
    // Navidate complete should have brought us to github
    Assert.assertEquals(navigateURL, listener.navigateComplete2String);
    listener.navigateComplete2Called = false;
    listener.navigateComplete2String = null;
    listener.blockNavigation = true;
    arguments = new VARIANT[] { new VARIANT(blockedURL) };
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(arguments);
    hr = ieDispatch.Invoke(dispIdNavigate.getValue(), niid, lcid, methodFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    // wait 10 seconds to ensure navigation won't happen
    for (int i = 0; i < 10; i++) {
        if (listener.navigateComplete2Called) {
            break;
        }
        Thread.sleep(1000);
    }
    // Naviation will be blocked - so NavigateComplete can't be called
    Assert.assertFalse("NavigateComplete Handler was called although it should be blocked", listener.navigateComplete2Called);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) EXCEPINFO(com.sun.jna.platform.win32.OaIdl.EXCEPINFO) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) VARIANT_BOOLByReference(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) PointerByReference(com.sun.jna.ptr.PointerByReference) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS) Test(org.junit.Test)

Example 8 with LCID

use of com.sun.jna.platform.win32.WinDef.LCID in project jna by java-native-access.

the class ComEventCallbacks_Test method after.

@After
public void after() {
    // Shutdown Internet Explorer
    DISPPARAMS.ByReference pDispParams = new DISPPARAMS.ByReference();
    VARIANT.ByReference pVarResult = new VARIANT.ByReference();
    IntByReference puArgErr = new IntByReference();
    EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    HRESULT hr = ieDispatch.Invoke(dispIdQuit.getValue(), niid, lcid, methodFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    ieDispatch.Release();
    Ole32.INSTANCE.CoUninitialize();
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) EXCEPINFO(com.sun.jna.platform.win32.OaIdl.EXCEPINFO) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) VARIANT_BOOLByReference(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS) After(org.junit.After)

Aggregations

WString (com.sun.jna.WString)5 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)5 PointerByReference (com.sun.jna.ptr.PointerByReference)5 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)4 LCID (com.sun.jna.platform.win32.WinDef.LCID)4 CLSID (com.sun.jna.platform.win32.Guid.CLSID)2 REFIID (com.sun.jna.platform.win32.Guid.REFIID)2 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)2 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)2 VARIANT_BOOLByReference (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference)2 DISPPARAMS (com.sun.jna.platform.win32.OleAuto.DISPPARAMS)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 UINTByReference (com.sun.jna.platform.win32.WinDef.UINTByReference)2 IntByReference (com.sun.jna.ptr.IntByReference)2 Before (org.junit.Before)2 COMException (com.sun.jna.platform.win32.COM.COMException)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1 ComExcel_Application (com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application)1 ComIApplication (com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication)1 ComIWorkbook (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook)1