Octave: Unable to pass optional arguments into VBA function (when adding excel sheets via COM server)

Viewed 83

I'm attempting to add sheets to an excel file.

It should be fairly straightforward; however, the following minimal working example below fails when attempting to skip optional arguments using empty braces. (Is this not correct? Source 1 Source 2)

clc
clear

% if COM error occurs, excel process remains open.
% use task manager to end process, else 'a.xlsx' file remains "in use".

srvr = actxserver('excel.application');
wbks = srvr.workbooks;

pth  = fullfile(pwd, 'a.xlsx');

if ~exist(pth, 'file')

  wbk = wbks.add;
  wbk.activate;
  wbk.saveAs( pth );
  wbk.close;

end

wbk = wbks.open( pth, 0, false );
wbk.activate;

wshts    = wbk.worksheets;

shts     = wbk.sheets;

wsht     = wshts.item(1);
wsht.activate;

sht      = shts.item(1);

wsht.select(true);
sht.select(true);

%{
https://docs.microsoft.com/en-us/office/vba/api/excel.sheets.add
https://docs.microsoft.com/en-us/office/vba/api/excel.worksheets.add
%}

shts.add(sht);     % functions
shts.add(wsht);    % functions

shts.add([],sht);  % fails
shts.add([],wsht); % fails


shts.count

wbk.save;

srvr.quit;

Here is the error:

error: com_invoke: property/method invocation on the COM object failed with error `0x800a03ec' - Z
error: called from
    trash at line 46 column 1

Note that the add functions until skipping the first input. Are square brackets the wrong method to skip an input?

Original source.

I have also posted this in Octave forum.

1 Answers
Related