Search code examples
excelc#-4.0vstooffice-interopribbon

Determine Office version in VSTO 4 before this.Application is set


I would like to determine the version of Office/Excel in a VSTO Addin when CreateRibbonExtensibilityObject() is called on the Addin. I have encountered an issue with this, and have encountered:

  • the this.Application of the addin is null, it is not yet set by VSTO at this time.
  • the ThisAddIn_Startup(..) is called after the CreateRibbonExtensibilityObject().

this.Application.Version is not available yet as the Addin seems not yet initialized at this time. Is there a way to determine the version of Excel (12, 14, or 15) at the time when the VSTO runtime calls CreateRibbonExtensibilityObject() on the Addin?

UPDATE

Finding that the ItemProvider was instantiated, I used the following to get the major office version.

FieldInfo temp = this.ItemProvider.GetType().GetField("_officeVersion", BindingFlags.NonPublic | BindingFlags.Instance);
uint officeVersion = (uint)temp.GetValue(this.ItemProvider);

I am accepting SliverNinja's answer too.


Solution

  • You can use System.Diagnostics to access the currently running Office process (excel.exe), grab the path to the process filename (MainModule), then use FileInfoVersion to determine the major product version.

    int majorVersion = FileVersionInfo.GetVersionInfo(Process.GetCurrentProcess().MainModule.FileName).ProductMajorPart;