Can I still use Microsoft.Office.Interop assemblies with office 2013?
Asked Answered
L

5

53

I had to import an older project (in .Net 2) into Visual Studio 2013, it makes use of the Microsoft Primary Interop Assemblies.

Visual Studio said that I need to add references to the project. Now I went and did some reading and apparently Microsoft has only released the PIA for office 2010? (I have Office 2013)

Now what I would like to know is.

  1. Can I get it to work with office 2013 and be backward compatible?
  2. And if so is this a good route to go for the future? Is it going to be compatible? Because I see you need .Net 2 (at the latest) and Windows 8 comes with 4.5 and not 3 (by default) and most new computers are going to have Office 2012 or 2013.
Lammergeier answered 9/1, 2014 at 7:17 Comment(1)
I faced the same problem and I got a resolution which worked from: microsoftsupportchat.com/blog/post/…Composed
P
131

PIAs are a historical artifact, required only by old .NET versions (before v4). They have been thoroughly and elegantly replaced by the "Embed Interop Types" feature, also known as the "No PIA" feature. Supported since Visual Studio 2010, you'll find it back in the Properties window when you select a reference assembly. It defaults to True. A good video that covers the underlying technology is available here.

Which is the reason that Microsoft doesn't publish the PIAs for Office 2013, they expect you to embed the interop types instead.

The feature is very desirable, it avoids your customer having to install the PIAs on his machine and for you to include them with your installer. Solving the issue when neither takes care of it, an entirely too common mishap. In addition, the PIAs for Office are very large, the great advantage of embedding the interop types is that your assembly only contain the types that you actually use. Many megabytes reduced to a few kilobytes.

The workflow is a little different. Instead of adding a reference to the Microsoft.Office.Interop assemblies as available in the Add Reference dialog, .NET Framework tab, you now use the COM tab. And pick, say, "Microsoft Excel 15.0 Object Library" to generate the interop types for a program that uses Excel. If you load an old project that previously used PIAs then just remove those reference assemblies and add them back from the COM tab.

Do note that a feature is lost, intentionally targeting an old version of Office that you don't actually have installed on your dev machine is more difficult. If that's a requirement then you still need the PIAs for that version, force the Embed Interop Types to True in the Properties window. Actually doing this is questionable, Microsoft has a hard time keeping new Office versions completely compatible with old versions. They've kept it up for 15 years now but it has been running out of steam. A worst-case scenario is targeting a newer version than you have installed on your machine, that's liable to make your program crash with very hard to diagnose exceptions like AccessViolationException.

Do note that you have to make small modifications to your code to allow it to work. The synthetic "XxxxClass" classes are not embedded, just the "Xxxx" interfaces. Simply remove the word "Class" from the new statement.

Psychotomimetic answered 9/1, 2014 at 11:5 Comment(8)
Hey... I am creating MS Word reporting. Have I sue OpenXML format for the modern version of MS Word of I have use Microsoft.Office.Interop.Word ? Could you please clarify it? THanks!Sclerometer
My only objection to this answer is that PIA's for Office 2013 ARE still published - for example, if you have VS2013 installed, you will find them at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15Tilbury
That's just part of the history. If you take a closer look at them with, say, ildasm.exe then you'll see they target .NET 2.0.50727. Type embedding requires .NET 4. They are only there because VSTO still supports targeting earlier .NET versions. Deploying them is squarely your job, the client cannot help.Psychotomimetic
If I use the new reference (from COM) will it work on computer that has office 2007 installed (mine has office 2016)Parliamentarianism
can we develop office 2013 add-in without using PIA ?Rosser
Will it work if I have to deploy my app to computers running different older versions of Office?Corticate
Hmmm. How are PIA supposed to be "a historical artifact" that has been "thoroughly and elegantly replaced", if there is no way to support multiple Office versions without them..?Avon
Not understanding what PIAs were supposed to do is common, do watch the video.Psychotomimetic
U
5

VS 2015 Community with Office 365 - for whatever reason the Add from the COM object does not work. The solution is go into the GAC and find the interop assemblies, copy them to a temp directory, then add to your project like any DLL.

Uella answered 26/9, 2016 at 14:58 Comment(0)
O
1

Also, if you don't know by now, Windows 8 does have older versions of .NET Framework but they aren't installed by default. Go to Program Features ---> Add features to Windows ----> and the first check box should be .NET 3.0 and maybe 2.0. Note, if you are on a WSUS server you will need to tell Windows to grab the files from the Windows Update servers. Hope it helps!

Okie answered 16/7, 2014 at 14:28 Comment(0)
F
0

Officially there is no backward compatibility of the PIA for Office. In fact it works.

For backward compatibility reasons I'm using the PIA for Office XP since several years and it works fine with Office XP, 2003, 2007 and 2010 (not yet tested with 2013) and on Windows XP, Vista, 7 and 8.

For compatibility with the different versions of Windows I'm using the .NET framework 3.5.

For the future... it depends of what you are doing with the PIA. If possible it is far better to directly deal with the Open XML files or to create an add-in for word/excel.

Favian answered 9/1, 2014 at 9:50 Comment(1)
OpenXML is NOT a replacement! The documentation is a shame. Sample code is impossible to find. (Try to obtain the background color of a cell in OpenXML! Have fun!) But mainly OpenXML is not a solution because none of the formulas is executed. So if a cell's content is filled by a formula you will not know what value the cell has unless you use real Office automation.Phenylalanine
F
0

I just found out that the Visual Studio 2013 express does not have office-support anymore. So you need at least the pro-version to make it work.

Farthest answered 11/8, 2015 at 11:54 Comment(1)
I'm using Visual Studio 2015 Community Edition. It also didn't have Office support. However, for VS 2015 it can be downloaded from aka.ms/officedevtoolsforvs2015 The article that explains this is here: blogs.msdn.microsoft.com/visualstudio/2016/04/26/…Calamite

© 2022 - 2024 — McMap. All rights reserved.