Search code examples
exceldelphiexcel-automation

Detect Version of Excel in Delphi


I am currently using the following code to check to see if the Excel Automation libraries exist on the user's computer:

  CoInitialize(nil);
  ExcelExists := true;
  try
    TestExcel := CreateOleObject('Excel.Application');
  except
    ExcelExists := false;
  end;
  if ExcelExists then begin
    TestExcel.Workbooks.Close;
    TestExcel.Quit;
    TestExcel := Unassigned;
  end;

This has been working fine until one user only had Excel 2003. The above code said he had Excel, however my Excel automation does not work for him, and I suspect it won't work for versions prior to Excel 2003 either.

How can I check that the version of Excel that is installed is 2007 or later?


Based on David's answer, I ended up putting this after the if ExcelExists statement and it seems to do the job:

S := TestExcel.Application.Version;
if (copy(S, 3, 1) <> '.') or (S < '12') then
  ExcelExists := false;

Version 12 was Office 2007.


Solution

  • Read the Version property of the Excel Application object. Compare this against the minimum value that your code supports.