Excel – Allow editing from MATLAB while sheet still open

  excel, matlab, windows

I’m trying to create an add-in for Excel with Matlab, where the end goal is to import data and graphs.
I’ve managed to paste the current figure in Matlab to Excel using the code below:

function xlsPasteToCOPY(filename,sheetname,width, height,varargin)
%Paste current figure to selected Excel sheet and cell
%
%
% xlsPasteTo(filename,sheetname,width, height,range)
%Example:
%xlsPasteTo('File.xls','Sheet1',200, 200,'A1')
% this will paset into A1 at Sheet1 at File.xls the current figure with
% width and height of 200
%
% [email protected]
options = varargin;
range = varargin{1};

[fpath,file,ext] = fileparts(char(filename));
if isempty(fpath)
fpath = pwd;
end
Excel = actxserver('Excel.Application');
set(Excel,'Visible',0);
Workbook = invoke(Excel.Workbooks, 'open', [fpath filesep file ext]);
Workbook.ChangeFileAccess(2);
sheet = get(Excel.Worksheets, 'Item',sheetname);
invoke(sheet,'Activate');
ExAct = Excel.Activesheet;

ExActRange = get(ExAct,'Range',range);
ExActRange.Select;
pos=get(gcf,'Position');
set(gcf,'Position',[ pos(1:2) width height])
print -dmeta
invoke(Excel.Selection,'PasteSpecial');
invoke(Workbook, 'Save');
invoke(Excel, 'Quit');
delete(Excel);

However this only works when the said Excel sheet is closed. I need a way to allow editing while it’s still open. Is this possible?

It works perfectly while the Excel-sheet is closed. But when kept open, I receive this error message:
Error

Source: Windows Questions

LEAVE A COMMENT