Package com.jacob.activeX

Examples of com.jacob.activeX.ActiveXComponent


  /**
   * example run loop method called by main()
   */
  public void runMonitor() {

    ActiveXComponent wmi = null;
    wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
    // no connection parameters means to connect to the local machine
    Variant conRet = wmi.invoke("ConnectServer");
    // the author liked the ActiveXComponent api style over the Dispatch
    // style
    ActiveXComponent wmiconnect = new ActiveXComponent(conRet.toDispatch());

    // the WMI supports a query language.
    String query = "select CategoryString, Message, TimeGenerated, User, Type "
        + "from Win32_NtLogEvent "
        + "where Logfile = 'Application' and TimeGenerated > '20070915000000.000000-***'";
    Variant vCollection = wmiconnect
        .invoke("ExecQuery", new Variant(query));

    EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());

    String resultString = "";
View Full Code Here


   *            the drive to run the test against
   */
  public void setUp(String drive) {
    if (fileSystemApp == null) {
      ComThread.InitSTA();
      fileSystemApp = new ActiveXComponent("Scripting.FileSystemObject");
      myDrive = Dispatch.call(fileSystemApp, "GetDrive", drive)
          .toDispatch();
    }
  }
View Full Code Here

   *
   * ----------------------------------------------------------------------------------------------------------------------------
   */
  public void testLeakWithSetString() {

    ActiveXComponent xl = null;
    Dispatch workbooks = null;
    Dispatch workbook = null;
    Dispatch workSheets = null;
    Dispatch sheet = null;
    Dispatch tabCells = null;
    SafeArray sa = null;

    // -Dcom.jacob.autogc=true
    System.out.println("Jacob version: " + JacobReleaseInfo.getBuildVersion());

    for (int t = 0; t < 10; t++) {
      // look at a large range of cells
      String position = "A7:DM8934";

      try {
        xl = new ActiveXComponent("Excel.Application");
        System.out
            .println("Excel version=" + xl.getProperty("Version"));

        xl.setProperty("Visible", new Variant(false));
        workbooks = xl.getProperty("Workbooks").toDispatch();

        workbook = Dispatch.get(workbooks, "Add").toDispatch();

        workSheets = Dispatch.get(workbook, "Worksheets").toDispatch();

        sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
        // grab the whole range specified above.
        tabCells = Dispatch.invoke(sheet, "Range", Dispatch.Get,
            new Object[] { position }, new int[1]).toDispatch();

        sa = Dispatch.get(tabCells, "Value").toSafeArray(true);

        System.out.println("Ub0=" + sa.getUBound(1)); // nbCol
        System.out.println("Ub1=" + sa.getUBound(2)); // nbLgn

        // number of rows
        int nbLgn = sa.getUBound(2);
        // number of columns
        int nbCol = sa.getUBound(1);

        int[] colLgn = new int[] { 0, 0 };

        // now set a value on every cell in the range we retrieved
        for (int i = 1; i <= nbLgn; i++) {
          colLgn[1] = i;

          for (int j = 1; j <= nbCol; j++) {
            colLgn[0] = j;
            // this one works with out a leak 1.13-M3
            // sa.setString(j, i, "test");
            // This one leaks with 1.13-M3 and earlier
            sa.setString(colLgn, "test");
          }
        }

        Dispatch.put(tabCells, "Value", sa);

        Variant f = new Variant(false);
        Dispatch.call(workbook, "Close", f);
        System.out.println("Close");
      } catch (Exception e) {
        e.printStackTrace();
      } finally {

        if (sa != null) {
          try {
            sa.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            sa = null;
          }
        }

        if (tabCells != null) {
          try {
            tabCells.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            tabCells = null;
          }
        }

        if (sheet != null) {
          try {
            sheet.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            sheet = null;
          }
        }

        if (workSheets != null) {
          try {
            workSheets.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            workSheets = null;
          }
        }

        if (workbook != null) {
          try {
            workbook.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            workbook = null;
          }
        }

        if (workbooks != null) {
          try {
            workbooks.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            workbooks = null;
          }
        }

        if (xl != null) {
          try {
            xl.invoke("Quit", new Variant[] {});
          } catch (Exception e) {
            e.printStackTrace();
          }

          try {
            xl.safeRelease();
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            xl = null;
          }
View Full Code Here

public class SafeArrayDispatchTest extends BaseTestCase {
  public void testDispatchWithSafeArray() {
    try {
      String scriptCommand = "1+(2*4)-3";
      String lang = "VBScript";
      ActiveXComponent sControl = new ActiveXComponent("ScriptControl");
      Dispatch.put(sControl, "Language", lang);

      Variant result = Dispatch.call(sControl, "Eval", scriptCommand);
      assertTrue(result.toString().equals("6"));

View Full Code Here

   * Runs the print ant lets the user say ok or cancel. Note the funky Visio
   * behavior if someone hits the cancel button
   *
   */
  public void testPrintDialog() {
    ActiveXComponent oActiveX = new ActiveXComponent("Visio.Application");
    Dispatch oDocuments = oActiveX.getProperty("Documents").toDispatch();
    // create a blank document
    Dispatch.call(oDocuments, "Add", "");
    try {
      Dispatch.call(oActiveX, "DoCmd", new Integer(1010)).getInt();
      System.out.println("User hit the ok button.");
    } catch (ComFailException e) {
      System.out.println("User hit the cancel button: " + e);
    } finally {
      oActiveX.invoke("Quit");
    }
    return;
  }
View Full Code Here

   *
   * @param filename
   */
  public void open(String filename) {
    // Instantiate objWord
    objWord = new ActiveXComponent("Word.Application");

    // Assign a local word object
    wordObject = objWord.getObject();

    // Create a Dispatch Parameter to hide the document that is opened
View Full Code Here

   *            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();
    }
  }
View Full Code Here

   * written
   */
  public void runTest() {
    // deprecated
    // System.runFinalizersOnExit(true);
    Dispatch test = new ActiveXComponent("MathTest.Math");
    TestEvents te = new TestEvents();
    DispatchEvents de = new DispatchEvents(test, te);
    if (de == null) {
      System.out
          .println("null returned when trying to create DispatchEvents");
View Full Code Here

   * @throws Exception
   */
  public static void main(String asArgs[]) throws Exception {
    System.out.println("Outlook: IN");

    ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
    try {
      System.out.println("version=" + axOutlook.getProperty("Version"));

      Dispatch oOutlook = axOutlook.getObject();
      System.out.println("version=" + Dispatch.get(oOutlook, "Version"));

      Dispatch oNameSpace = axOutlook.getProperty("Session").toDispatch();
      System.out.println("oNameSpace=" + oNameSpace);

      recurseFolders(0, oNameSpace);

    } finally {
      axOutlook.invoke("Quit", new Variant[] {});
    }
  }
View Full Code Here

  /**
   * verifies that messages can now have unicode in them like when the file
   * names have unicode characters
   */
  public void testUnicodeCharactersInErrorMessage() {
    ActiveXComponent application = new ActiveXComponent("Word.Application");
    ActiveXComponent documents = application
        .getPropertyAsComponent("Documents");
    String fileName = "abc\u0411\u0412\u0413\u0414def";
    try {
      documents.invoke("Open", fileName);
      fail("Should have thrown an exception");
    } catch (ComException e) {
      assertTrue("Error message should contain file name with unicode "
          + "characters in it. " + e.getMessage(), e.getMessage()
          .indexOf(fileName) > 0);
View Full Code Here

TOP

Related Classes of com.jacob.activeX.ActiveXComponent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.