0

I am trying to saveas excel file, but it gives an error: Can't map name to dispid: FileSaveAs.

private ActiveXComponent objExcel = new ActiveXComponent("Excel.Application");
excelObject = objExcel.getObject();
Dispatch.put(excelObject, "Visible", new Variant(false));
workbook = objExcel.getProperty("Workbooks").toDispatch();
workbook = Dispatch.call(workbook, "Open", filename).toDispatch();
Dispatch oExcel = Dispatch.call(objExcel, "Workbooks").getDispatch();
Dispatch.call(workbook, "FileSaveAs", filename, new Variant(51));

The Parameter of the variant is the taken from: http://msdn.microsoft.com/en-us/library/bb241279(v=office.12).aspx

Can anybody tell me where is the problem in code? Thanks

Probe
  • 1
  • 1
  • 1

3 Answers3

1

I'm not sure if I use the correct way to do it, but it works

public Boolean savesAS(String path, String nameFile){

    ComThread.InitSTA();

    ActiveXComponent excel = new ActiveXComponent("Excel.Application");
    excel.setProperty("Visible", new Variant(true));
    Object workbooks = excel.getProperty("Workbooks").toDispatch();
    Object workbook;

    try {            
        workbook = Dispatch.call((Dispatch)workbooks, "Open", path).toDispatch();           
    } catch (Exception e) {
        showMessageDialog(null,"Unable to open " + path);
        return false;
    }

    try {
        //here you can modify the value of "new Variant(int)" with other number
        Dispatch.call((Dispatch)workbook, "SaveAs", nameFile ,new Variant(2)); 
    } catch (Exception e) {
         showMessageDialog(null,"Unable to convert " + path);
        return false;
    }

    Dispatch.call((Dispatch)workbook, "Close", new Variant(false));

    excel.invoke("Quit", new Variant[0]);
    excel.safeRelease();
    ComThread.Release();

    return true;
}
gvlasov
  • 18,638
  • 21
  • 74
  • 110
LDG
  • 11
  • 3
0

As stated by the error it is not able to find the method "FileSaveAs". I tried to look for it in Google/Documentation and could not find it (maybe it exists but I could not find it). I found SaveAs instead. So try to use that and see what happens.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
0

Yes you are right, it does work with "Saveas". Actually "FileSaveAs" works with word document, so i thought to try it with Excel and Powerpoint, but it dint worked...

To saveas excel workbook, this code works:

 Dispatch.call(workbook, "SaveAs", filename, new Variant(1)); 
Probe
  • 1