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 件のコメント
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 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!