If/when driving Excel or other apps via COM/ActiveX, "dot" syntax isn't always available for every method and one is forced tor resort to invoke 'ing a method. This generally isn't too bad to figure out the syntax needed from the VBA documentation and is usually more-or-less straightforward when the optional parameters are constants and/or there is an example that shows calling syntax.
An example there that is pretty simple but works is to open or save a file--here's a code snippet that works to create a new workbook from a template to subsequently be populated--
...
xlWorkbookDefault =51;
xlLocalSessionChanges= 2;
isActive=false;
try
Excel = actxGetRunningServer('Excel.Application');
catch
Excel = actxserver('Excel.Application');
isActive=true;
end
Excel.Workbooks.Open(fullfile(templatedir,'Reconciliation.xltx'));
WBk=Excel.ActiveWorkbook;
Filename=fullfile(outdir,'Reconciliation.xlsx');
FileFormat=xlWorkbookDefault;
ConflictResolution=xlLocalSessionChanges;
invoke(WBk,'SaveAs',Filename,FileFormat,[],[],[],[],[],ConflictResolution)
WBk.Close
...
works quite nicely. All the parameter values are constants and passed by position.
If the parameter is another object, however, I've been unable to decipher how to pass to the method, The next step after the above is that there is a need to create a variable number of pages of the template depending on number of cases to be analyzed. I've run into an impasse at this point, however
<https://docs.microsoft.com/en-us/office/vba/api/excel.sheets.copy> shows syntax as
expression. Copy( _Before_ , _After_ )
expression A variable that represents a Sheets object.
where the parameter is set to the target of which sheet it is that is to be either before or after. The VBA example code is
Worksheets("Sheet1").Copy After:=Worksheets("Sheet3")
But I can't figure out any syntax that works that lets me specify the argument...
actSheet=Workbook.ActiveSheet;
I figured by similarity
invoke(actSheet,'Copy','Before',actSheet)
would be piece of cake but
K>> invoke(actSheet,'Copy','Before',actSheet)
Error using Interface.000208D8_0000_0000_C000_000000000046/Copy
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Copy method of Worksheet class failed
Help File: xlmain11.chm
Help Context ID: 0
K>>
I've tried every pertubation I can think of and have proven that leaving off the parameter entirely creates a copy of the page in a new workbook as is documented but I can't make it work in the same workbook.
Anybody have enough experience to know the magic incantation?