Reputation: 175
With the PIA's(https://learn.microsoft.com/en-us/visualstudio/vsto/office-primary-interop-assemblies?view=vs-2017) i could easly open word files and do Tasks in it. But the PIA's are not available anymore for Office 2016. My question is, is there an alternative? There is now the "Office Developer Tools for Visual Studio". Can i use that?
How it works with the PIA's:
Application app = null;
Document doc = null;
app = new Microsoft.Office.Interop.Word.Application();
doc = Microsoft.Office.Interop.Word.Documents.Open(filePath);
// .. do something
doc.Close();
app.Quit();
Is it possible to do similar things with Microsft.Office.Tools.Word from "Office Developer Tools for Visual Studio"?
Upvotes: 0
Views: 1330
Reputation: 25663
All versions of Office from 2007 onwards distribute PIAs with the Office installation; since Office 2010 they are also installed by default. They're still present in Office 2016/365 (and in Office 2019).
There was some question, early on, whether PIAs should be installed with Office, which is why there are/were "distributables" available for .NET solutions. The .NET solution would check whether the PIAs required were installed and, if not, intall them. Since that time, installing PIAs on end-user machines is no longer an issue - it's done by default. For this reason, Microsoft no longer provides distributables - the PIAs should be there. So, distributables have been discontinued... because they're no longer needed.
VSTO (Visual Studio Tools for Office - the "Tools" namespace in the question) relies on the PIAs, as do all other IDTExtensibility2 Add-ins and all COM applications that use the "interop".
It is possible to generate a custom set of IAs to distribute with an application, to make that application independent of the PIAs. The difference between PIAs and IAs is that PIAs are always provided by the software manufacturer and are optimized. COM object model syntax may vary (not work the same) for IAs as with PIAs. There are articles "out there" about creating and using IAs, but the starting point is the Visual Studio tool tlbImport.exe
.
Code can also automate Office applications with no IAs at all by using late-binding (PInvoke). This is quite a bit of work and a deep understanding of the Office libraries is required (no Intellisense). But it is possible. (And it's the work-around for any type library features that for some reason aren't included in the PIAs.)
Upvotes: 1