Opening an excel file, writing to it and closing it does not

75 vues (au cours des 30 derniers jours)
Branko Celler
Branko Celler le 9 Déc 2025 à 8:25
Commenté : dpb le 16 Déc 2025 à 13:12
% Name existing Excel file
ExcelOut='c:\UNSW\RESEARCH_ACTIVE\BP_Research\BP_UP_DOWN_INVASIVE\CRH_Data_NEW_10H.xlsx';
excelWorkbook=Excel.workbooks.Open(ExcelOut) % Open Excel
RANGE=RowCol(in+4,23); % Subroutine to define start of where to begin writing
writecell(A,ExcelOut,'Sheet',1,'Range',RANGE,'UseExcel',true,'WriteMode','inplace'); % Write to file
Excel.ActiveWorkbook.Save(); % Save Excel File
excelWorkbook.Close(false); % Close Excel File
Excel.Quit; % Quit Excel file. Release resource
Get multiple error messages in MATLAB R2020b
ie
Unable to resolve the name Excel.ActiveWorkbook.Save.

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Déc 2025 à 14:00
excelWorkbook=Excel.workbooks.Open(ExcelOut) % Open Excel
Excel.ActiveWorkbook.Save(); % Save Excel File
The names must match. You need
excelWorkbook.ActiveWorkBook.Save();
However, as @dpb noted, writecell() operates independently of activex operations.
  7 commentaires
Branko Celler
Branko Celler le 9 Déc 2025 à 23:06
Hi dpb,
your last comment prompted me to write the command
system('taskkill /F /IM EXCEL.EXE')
at the very start of my program of 1691 lines of code to kill any EXCEL processes and it worked!
my FOR loop is now writing correctly to the five different excel files.
THank you so much for your help!
dpb
dpb le 9 Déc 2025 à 23:30
" to write the command system('taskkill /F /IM EXCEL.EXE') at the very start of my program"
I would strongly recommend to NOT leave that permanently; once you killed the zombie processes that had the other files locked, there's no need and it will add to the overall overhead to have to restart Excel.

Connectez-vous pour commenter.

Plus de réponses (3)

dpb
dpb le 9 Déc 2025 à 12:55
Modifié(e) : dpb le 9 Déc 2025 à 14:57
You're mixing high-level MATLAB sritecell with code for direct ActiveX/COM interaction with Excel.
You don't need ActiveX here, anyway, writecell and the others (-table, -matrix, ...) handle all the opening and closing of the Excel file internally. Just
% Name existing Excel file
ExcelOut='c:\UNSW\RESEARCH_ACTIVE\BP_Research\BP_UP_DOWN_INVASIVE\CRH_Data_NEW_10H.xlsx';
RANGE=RowCol(in+4,23); % Subroutine to define start of where to begin writing
writecell(A,ExcelOut,'Sheet',1,'Range',RANGE,'UseExcel',true,'WriteMode','inplace'); % Write to file
is all you need.
Also take out the other ActiveX code not shown unless you are going to do things such as formatting the sheet in ways that aren't supported by the builtin read/write functions. If that is the intent, then wait until after have written the data and then open the file with ActiveX or forego using writecell and do it all with ActiveX.

Image Analyst
Image Analyst le 9 Déc 2025 à 14:40
See attached Excel Demo using ActiveX.

dpb
dpb le 13 Déc 2025 à 17:46
You don't still have some old ActiveX processes hanging around from having crashed Excel when you were trying to use it, by any chance?
Close all open Excel workbooks and then open the Task Manager and kill all Excel processes that may still be hanging around; then try again.
  2 commentaires
Branko Celler
Branko Celler le 16 Déc 2025 à 3:15
This was indeed the problem and using the command
system('taskkill /F /IM EXCEL.EXE')
Resolved the problem
dpb
dpb le 16 Déc 2025 à 13:12
Vote!! <VBG>
As noted earlier, however, do NOT keep that in your code once you cleaned up the processes with the locked files. Only do this when you've been messing with ActiveX and have crashed leaving the file you were working on inaccessible through ActiveX session.
BTW, you may want/need to go to your working directory and find those file references with the hidden attribute and the "~$" prefix Excel creates for an open file. Don't have any open files when doing this...

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by