6

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.

Dan
  • 854
  • 8
  • 21
  • The answer to [another question](https://stackoverflow.com/questions/28925050/how-to-know-which-word-version-is-installed-from-my-add-in-ribbon-with-c-shar) suggests the premise of this question (that it can't be retrieved in the startup method) is no longer valid for an application-level add-in. I tried it in a document-level solution in the startup method too, and it was fine. – Chris Oct 06 '17 at 17:51
  • @Chris, that has nothing to do with this question. CreateRibbonExtensibilityObject is called prior to the startup method.This question specifically asked how to get the version prior to the the startup method. – Dan Oct 06 '17 at 21:24
  • Ah, my mistake. – Chris Oct 09 '17 at 15:01

2 Answers2

5

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;
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
0

I'm using the FileVersionInfo of the MainModule. I hope this saves the file access to extract the version information.

int majorVersion = Process.GetCurrentProcess().MainModule.FileVersionInfo.ProductMajorPart;
jramos
  • 128
  • 1
  • 6