The following MATLAB code works well for writing
eActiveSheetRange.Value
to the range
eActiveSheetRange
However, I have to keep the respective Excel file not opened by any application to make the writing process successful.
So, is there any way to keep the Excel file opened by, for example, Excel while making the MATLAB able to write to it without throwing errors and watch the new values be written?
excel = actxserver('Excel.Application');wbooks = excel.Workbooks;eWorkbook = wbooks.Open('C:\Users\Diaa\Desktop\test.xlsx');eSheets = excel.ActiveWorkBook.Sheets;sheet1 = eSheets.get('Item',1);sheet1.ActivateeActiveSheetRange = get(excel.Activesheet,'Range','B1');eActiveSheetRange.Value = 15;eWorkbook.Saveexcel.Quitexcel.delete
Edit 1
Following @J. Alex Lee's answer, I get this error
when running the following code while the file is opened by Excel at the same time:
FileName = 'C:\Users\Diaa\Desktop\test.xlsx';SheetName = 'Sheet1';SheetNum = find( sheetnames(FileName) == SheetName);try excel = actxserver('Excel.Application');catch excel = actxGetRunningServer('Excel.Application');endexcel.Workbooks.Open(FileName).Sheets.Item(SheetNum).Activateexcel.Activesheet.Range('B1').Value = 88;excel.Activeworkbook.Saveexcel.Quitexcel.delete
Best Answer