Why using actxserver will turn my file to read-only

18 views (last 30 days)
Kat Lee
Kat Lee on 2 May 2019
Commented: Guillaume on 3 May 2019
Hey guys,
Read-only error been showed while I was trying to movefile, but the new directories is either my Downloads folder or my Desktop folder.
Here is what I did:
I first loaded the Excel file that I'd like to modify, and then turn on the actxserver('Excel.Application'), and then apply action for that pre-loaded excel file.
But I got error while I was trying to movefile(oldfilePath, newfilePath): "Use the 'f' option to override". This error does not make sense since neither the new location I have selected shall not be read-only space,nor there is no duplicate file existed in that directory.
I think somehow there is a delay happened between Excel and MATLAB, in another word, I feel the modifies Excel file was not saved successfully.
Does anyone has ever met this problem before?
Walter Roberson
Walter Roberson on 2 May 2019
Why not use a SaveAs instead of a Save, placing the file directly in the desired location ?
It is possible (and likely) that the Excel Quit process is done asynchronously and takes time.

Sign in to comment.

Answers (1)

Guillaume on 2 May 2019
Edited: Guillaume on 2 May 2019
The line
cRowNames = templateExcel.Properties.RowNames;
would imply that templateExcel is a matlab table. Yet on the previous line, you have:
which, if templateExcel is indeed a table is meaningless.
Later on in your code, you use the variable targetTable which is undefined. So this will also cause an error. I suspect that that targetTable is meant to be templateExcel but it's hard to know since the code is not commented.
Note that it's much easier to write a table to an excel file directly with writetable rather than converting to a cell array and then using xlswrite.
Later on you use the variable sExcelClearRange which is also undefined. In any case, relying on the ActiveSheet being the correct sheet after you've just opened the file in excel is very iffy. You may very well clear a different sheet than intended.
is not valid. I suspect you meant
which could be combined with the previous Workbook.Save line to just:
workbook.Close(true); %true to save and close the file all at once.
invoke(Excel, 'Quit');
is simply
Guillaume on 3 May 2019
actxserver is in some way very simple. It just connects you to a COM process (in this case, Excel). All the clever stuff is implemented in Windows directly and the only thing matlab does is relay your instructions to Windows. So, if there is a problem it's nothing that matlab can do anything about. In this particular case, the problem would be with Excel.
However, I'm not convinced at all that you've found the problem. There is nothing special about the Download and Desktop directories. Windows does not put any extra protection on them. You may get extra protections on some files downloaded from the internet but that's independent of where they reside.
If the code you've posted is not the code you're using, that makes it difficult to identify problems, so please post the exact code you're using.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by