views:

764

answers:

3

I am trying to communicate with Excel from a Java/SWT application. I have been able to open a worksheet, open a file and save it but that's about it.

Can anyone point me to some documentation/examples for this? I especially need to know which commands are available. I did try to record macros to inspect. This was useful but did not give me everything I wanted.

This is a sample of what I have been trying so far:

private static OleAutomation openFile(
        OleAutomation automation, String fileName) {
 Variant workbooks = automation.getProperty(0x0000023c);// get User
               // Defined
               // Workbooks
 Variant[] arguments = new Variant[1];

 arguments[0] = new Variant(fileName);
 System.out.println("workbooks::\t" + workbooks);

 IDispatch p1 = workbooks.getDispatch();
 int[] rgdispid = workbooks.getAutomation().getIDsOfNames(new String[] { "Open" });
 int dispIdMember = rgdispid[0];
 Variant workbook = workbooks.getAutomation().invoke(dispIdMember, arguments);
 System.out.println("Opened the Work Book");
 try {
  Thread.sleep(500);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 int id = workbook.getAutomation().getIDsOfNames(new String[] { "ActiveSheet" })[0];
 System.out.println(id);
 Variant sheet = workbook.getAutomation().getProperty(id);
 OleAutomation sheetAutomation = sheet.getAutomation();

 return (sheetAutomation);
}
+1  A: 

Not a documentation, but since you asked about the available commands via automation: have you tried the OLE/COM Object viewer that comes with the Windows 2000 resource kit? Download here.

Stephan Keller
not yet - thanks for the link
paul
+1  A: 

Use VBA help MSOffice. Also you can use Object Browser in Office's VB editor.

P.Melamed