Overwriting an excel file with uiputfile
4 views (last 30 days)
Show older comments
With my script I am creating an Excel file, inserting an image and some data. The user can decide name and path of this file via uiputfile.
The problem is if I want to overwrite an existing Excel file I get the Windows message box "This file already exists, Do you want to replace it?" but I get also a similar message box from Excel.
Is there a way to avoid this second message box to show up?
This is my code:
[resultfilename, resultpath]=uiputfile('*.xlsx');
resultsave=fullfile(resultpath,resultfilename);
excel = actxserver('Excel.Application');
workbooks = excel.Workbook;
workbook = invoke(workbooks, 'Add');
sheets = excel.ActiveWorkbook.Sheets;
sheet1 = get(sheets, 'Item', 1);
sheet1.Activate;
sheet1.invoke('Pictures').Insert([pwd '\' img]);
sheet1.Range(range_res).NumberFormat = '0,000';
sheet1.Range(range_cap).EntireColumn.ColumnWidth = 12;
set(sheet1.Range(range_cap),'Value',caption);
set(sheet1.Range(range_res),'Value',result);
set(sheet1.Range(range_unit),'Value',unit);
invoke(workbook, 'SaveAs', resultsave);
invoke(excel, 'Quit');
winopen(resultfilename);
3 Comments
dpb
on 10 May 2019
Edited: dpb
on 10 May 2019
As the first link shows, it's the eighth argument in the argument list, I think you just string enough empty placeholders in the argument to invoke to get to the right position for the flag.
invoke(workbook, 'SaveAs', resultsave,[],[],[],[],[],[],2);
if I counted correctly and if it's empty [] instead of '' empty strings for placeholders or just a list of commas. I don't write enough COM to remember; if you DAGS you'll undoubtedly find some further examples of Matlab usage of invoke to see...or, I'm sure it's used in functions like XLSREAD/WRITE that you can go digging into source code to find usages there...
I can't seem to find a specific example at the moment--it looks like I used the workaround of
Wkbk.Save
Wkbk.Close
where the 'Save' operation eliminates the popup from being shown instead of forcing via the parameter.
Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!