views:

279

answers:

3

I have a c++ unmanaged project whose output is an .xll file which is an add-in loaded by excel at startup, this add-in can work with both versions, excel 2003 and excel 2007.

Now, what I need to do is to obtain the version of the excel instance that the user is actually using for working with my add-in.

Does anyone can suggest me how to get it?

Thanks

+1  A: 

This is maybe off-topic, but the GetFileVersionInfo function will return the VERSION_INFO struct in which you can use the VerQueryValue function to retrieve the file version. The downside is that you have to find the path to the executable, this can be done with the GetModileFileName function if you have the handle to the Excel instance.

Gabriella
+2  A: 

You can call Excel4(xlfGetWorkspace, &version, 1, &arg), where arg is a numeric XLOPER set to 2 and version is a string XLOPER which can then be coerced to an integer.

The result for Excel 2007 would be 12.

You can do this in xlAutoOpen.

Len Holgate
+2  A: 

Len Holgate gave the correct answer to find out the version number of Excel in use. Here's some sample code:

xloper xlstrVersion, xlintVersion, xlintParam;
// supply a parameter with value 2 to read the Excel version number as a string
xlintParam.xltype = xltypeInt;
xlintParam.val.w = 2;   
int xlret = Excel4(xlfGetWorkspace, &xlstrVersion, 1, &xlintParam);
// now use the xlCoerce function to convert the version string to an integer
xlintParam.val.w = xltypeInt;
xlret = Excel4(xlCoerce, &xlintVersion, 2, &xlstrVersion, &xlintParam);
const int ExcelVersion = xlintVersion.val.w;
const bool ExcelVersion12Plus = ExcelVersion >= 12;

If you only need to distinguish Excel version 12 from earlier versions, then use the XLCallVer function as follows:

const bool ExcelVersion12Plus = (0x0c00 == XLCallVer());
jumpalongjim
Thank you and Len, this was exactly what I was looking for.
Vic