Com Fail Exception

prioux
2012-04-23
2013-05-20
  • prioux

    prioux - 2012-04-23

    Hello.
    I have a problem with this code:

    package org.jbe.win32.jacob;  
    import com.jacob.activeX.ActiveXComponent; 
    import com.jacob.com.Dispatch; 
    import com.jacob.com.Variant;  
    public class DispatchTest 

    public static void main(String args) 

    ActiveXComponent xl = new ActiveXComponent("Excel.Application"); 
    Dispatch xlo = (Dispatch) xl.getObject(); 
    try { 
    System.out.println("version="+xl.getProperty("Version")); 
    System.out.println("version="+Dispatch.get(xlo, "Version")); 
    xl.setProperty("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).toDispatch(); 
    Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get, 
    new Object {"A2"}, 
    new int).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 {
    System.out.println("finally");
    xl.invoke("Quit", new Variant {}); 



    When I run it:
    com.jacob.com.ComFailException: A COM exception has been encountered:
    At Invoke of: Version
    Description: An unknown COM error has occured.
    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
    at org.jbe.win32.jacob.DispatchTest.main(DispatchTest.java:13)
    Exception in thread "main" com.jacob.com.ComFailException: A COM exception has been encountered:
    At Invoke of: Quit
    Description: An unknown COM error has occured.
    at com.jacob.com.Dispatch.invokev(Native Method)
    at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
    at org.jbe.win32.jacob.DispatchTest.main(DispatchTest.java:35)

    I am under XP pro version 2002 SP3,
    eclipse indigo,java SE-1.6 and jacob 1.16

     
  • Jarek Czekalski

    Jarek Czekalski - 2012-04-23

    I just ran your code on Win7 64 without problems, getting:

    version=11.0
    version=11.0
    a1 from excel:123.456
    a2 from excel:null
    finally
    

    Are you able to run succesfully the following file using command "cscript excel.vbs":

    set xl = CreateObject("excel.application")
    wscript.echo "excel: " & xl.version
    xl.workbooks.add
    xl.visible = -1
    xl.quit
    
     
  • prioux

    prioux - 2012-04-24

    Thank you.
    cscript excel.vbs is OK
    _>excel:12.0
    What can i do?

     
  • Jarek Czekalski

    Jarek Czekalski - 2012-04-24

    Either you make some mistake, like wrong jacob dll (from a different version) or this is a bug in jacob. I was using 1.15, so you may try another jacob version. If all fails, I would submit a bug report. Your code seems to be right.

    Probably there is a possibility of making jacob verbose, that's another thing to try. I don't remember the details.

     

Log in to post a comment.