Capture Excel Application in Matlab while opened on Desktop

조회 수: 1 (최근 30일)
Philosophaie
Philosophaie 2014년 1월 31일
편집: Image Analyst 2014년 1월 31일
Thre is an Excel Application already open on the Desktop. I want to capture it in MATLAB.
Line
exlFile = exlWkbk.Open(['C:/MATLAB7/work/path/file.xls']);
needs to be different.
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
exlFile = exlWkbk.Open(['C:/MATLAB7/work/Relativity/CentrpetalAcceleration.xlsm']);
exlSheet1 = exlFile.Sheets.Item('Sheet1');
%code here
exlWkbk.Close;
exl.Quit;

답변 (1개)

Image Analyst
Image Analyst 2014년 1월 31일
I think you want something like this
% Get the Excel COM server:
% Excel = actxGetRunningServer('Excel.Application')
Excel = actxserver('Excel.Application')
% Make it appear (it's invisible otherwise).
Excel.visible = true;
% Suppress Excel warning popups, like for overwriting a file.
Excel.DisplayAlerts = false;
% Open up the existing workbook named in the variable fullFileName.
excelFullFileName = 'C:\Users\Philosophaie\Documents\Spreadsheets\Expenses.xls'
if exist(excelFullFileName, 'file')
Excel.Workbooks.Open(excelFullFileName);
else
message = sprintf('Warning: %s is not there!', excelFullFileName);
uiwait(warndlg(message));
Excel.Close;
Excel.Quit;
clear('Excel') % Close our connection to Excel but don't shut it down. return;
end
% Code to do stuff with Excel.....
% Then finish up: save and close and delete object variable.
Excel.ActiveWorkbook.Save
Excel.ActiveWorkbook.Close
Excel.Quit
clear('Excel') % Close our connection to Excel but don't shut it down.
  댓글 수: 1
Image Analyst
Image Analyst 2014년 1월 31일
편집: Image Analyst 2014년 1월 31일
By the way, Quiting and clearing in the above code will shut down your workbook, but leave open any that were open before you started running this code. It doesn't shut down Excel for all workbooks, just those you opened in your MATLAB code.
Attached is a demo, in case anyone is interested, that does some more stuff like reading and writing via ActiveX. This is essential if you want to deal with multiple workbooks and don't want to wait forever for xlsread and xlswrite to work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 ActiveX에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by