Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 790
Dan Avatar asked Nov 20 '12 16:11

Dan


People also ask

How do I deploy VSTO add-ins in Outlook?

On the File menu, point to New, and then click Project. In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint. Under the expanded Office/SharePoint node, select the Office Add-ins node. In the list of project templates, choose an Outlook VSTO Add-in project.

How do you install VSTO add-in for all users?

Deployment type If you want to register a VSTO Add-in to all users on a computer, you must use Windows Installer to deploy the VSTO Add-in. For more information about these deployment types, see Deploy an Office solution by using ClickOnce and Deploy an Office solution by using Windows Installer.


1 Answers

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;
like image 182
SliverNinja - MSFT Avatar answered Oct 13 '22 11:10

SliverNinja - MSFT