Overwriting an excel file with uiputfile

4 次查看(过去 30 天)
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 个评论
banco freno
banco freno 2019-5-10
I think I found it, is it the ConflictResolution parameter? See here and here.
If it is the correct parameter, I still can't figure out what is the code to be included in my script.
dpb
dpb 2019-5-10
编辑:dpb 2019-5-10
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.

请先登录,再进行评论。

回答(0 个)

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by