How to check the version of Excel through Matlab?

3 次查看(过去 30 天)
Currently I'm using activeX to plot some charts and write some data on excel, however, when I try my code in a different computer the VBA method that I used to create the charts doesn't work. In my personal laptop I use the following line:
Chart=h.ActiveSheet.Shapes.AddChart2;
But when I used the computer at my office it only works with:
Chart=h.ActiveSheet.Shapes.AddChart;
I would like to generalize my code so it can detect which version of Excel is being used or just to make a check before calling the method, so that it doesn't show an error on the code.
Thanks in advance.

采纳的回答

Fangjun Jiang
Fangjun Jiang 2017-11-15
ExcelApp=actxserver('excel.application');
str2double(get(ExcelApp,'Version'))

更多回答(1 个)

Image Analyst
Image Analyst 2017-11-15
Use Excel.Version. See this snippet:
try
% See if there is an existing instance of Excel running.
% If Excel is NOT running, this will throw an error and send us to the catch block below.
Excel = actxGetRunningServer('Excel.Application');
% If there was no error, then we were able to connect to it.
catch
% No instance of Excel is currently running. Create a new one.
% Normally you'll get here (because Excel is not usually running when you run this).
Excel = actxserver('Excel.Application');
end
% Prepare proper filename extension.
% Get the Excel version because if it's version 11 (Excel 2003) the file extension should be .xls,
% but if later than version 12.0 (Excel 2007) then we'll need to use an extension of .xlsx to avoid nag messages.
excelVersion = str2double(Excel.Version);
if excelVersion < 12
excelExtension = '.xls';
else
excelExtension = '.xlsx';
end

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by