Borrar filtros
Borrar filtros

Overwriting an excel file with uiputfile

4 visualizaciones (últimos 30 días)
banco freno
banco freno el 10 de Mayo de 2019
Editada: dpb el 10 de Mayo de 2019
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 comentarios
banco freno
banco freno el 10 de Mayo de 2019
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 el 10 de Mayo de 2019
Editada: dpb el 10 de Mayo de 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.

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by