How to check the version of Excel through Matlab?

5 views (last 30 days)
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.

Accepted Answer

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

More Answers (1)

Image Analyst
Image Analyst on 15 Nov 2017
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

Products

Community Treasure Hunt

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

Start Hunting!