Effacer les filtres
Effacer les filtres

Capture Excel Application in Matlab while opened on Desktop

5 vues (au cours des 30 derniers jours)
Philosophaie
Philosophaie le 31 Jan 2014
Modifié(e) : Image Analyst le 31 Jan 2014
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;

Réponses (1)

Image Analyst
Image Analyst le 31 Jan 2014
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 commentaire
Image Analyst
Image Analyst le 31 Jan 2014
Modifié(e) : Image Analyst le 31 Jan 2014
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.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by