Search code examples
excelcomoctave

Octave: Unable to save excel file via COM function (COM error 0x800a03ec)


In Octave, this code:

excel.server    = actxserver('excel.application');
excel.workbooks = excel.server.workbooks;
excel.workbook  = excel.workbooks.add;
% excel.workbook.activate;
excel.workbook.SaveAs("a.xls");

results in a file being created at: C:/Users/kando/Documents/a.xls, whereas:

excel.server    = actxserver('excel.application');
excel.workbooks = excel.server.workbooks;
excel.workbook  = excel.workbooks.add;
% excel.workbook.activate;
excel.workbook.SaveAs('C:/Users/kando/Documents/a.xls');

results in the following error:

error: com_invoke: property/method invocation on the COM object failed with error `0x800a03ec' - lZ

I am thus unable to save anywhere when specifying an absolute or relative path.

(I am running the code from an entirely different directory, but the COM server only operates in the user's documents folder, it seems.)

How can I specify a path, (and how can I get more detailed error info when using COM server functions)?


Solution

  • You are using a unix-style path separator (i.e. a forward slash: /).

    Contrary to unix systems, formally the windows path separator is the backslash, i.e. \. Therefore, unless you are sure that the application you're passing this to is programmed flexibly so as to interpret both, you should probably be using a backslash specifically to ensure it's not treated as a 'malformed' path string when passed to windows applications.

    In other words, you should be using 'C:\Users\kando\Documents\a.xls' instead of 'C:/Users/kando/Documents/a.xls' as your path string.

    Better yet, you should use octave's fullfile facilities, which detects the correct file separator for you (via the filesep function), and builds an OS-compatible pathstring from the provided parts, i.e.

    SaveFile = fullfile( 'C:', 'Users', 'kando', 'Documents', 'a.xls' );
    excel.workbook.SaveAs( SaveFile );