Open a project referencing "MS-Office Object Library" on multiple development machines - c#

I'm using two computers, one has Office 2010 installed and the other has Office 2016. The first one is my main PC.
Let's say I started a project on the main PC. If I add a reference to any of the following:
Microsoft.Office.Interop.Word (Microsoft Word 14.0 Object Library).
Microsoft.Office.Interop.Excel (Microsoft Excel 14.0 Object Library).
Microsoft.Office.Interop.PowerPoint (Microsoft PowerPoint 14.0 Object Library).
..and then open the project on the second PC, Visual Studio will automatically select the applicable version for all of them (i.e., "Microsoft XXXX 16.0 Object Library") since "Embed Interop Type" property is set to true.
So far so good, but when I add a reference to Microsoft.Office.Core (Microsoft Office 14.0 Object Library), I can't get the same behavior, that is, when I open the project on the second PC, I find the reference with the same version (14.0) flagged with an icon indicating that the reference is missing along with warnings telling me the same thing (the reference cannot be found).
Obviously, if I remove the reference and replace it with the newer version (16.0), everything works fine except -of course- that I won't be able to open the solution on the main PC anymore.
So, my questions are:
Why doesn't it select the appropriate version for Microsoft.Office.Core like what happens with Interop.Excel, Interop.Word, etc?
Is there a solution for this so I can open the project seamlessly between the two computers?
Please note:
The main focus is not about running the application by the end user with different versions of Office (this have been asked and answered many times before). I'm more concerned about opening the solution/project on a secondary development machine with a different version of Office installed.
In case that's relevant, this is a WinForms application.
Update:
I tried copying the MSO.DLL file to a local 'lib' folder but whenever I add it as a reference, it shows "C:\Windows\assembly\GAC_MSIL\Office\14.0.0.0__71e9bce111e9429c\Office.dll" as the path of the reference. Then, no matter whether I disable/enable the Embed Interop Types property, it still doesn't compile on the secondary development machine.
Although I'm not quite sure I fully get Eugene's answer, it helped by giving me the idea of adding the PIA references in the Assemblies tab, not the COM tab and still have the Embed Interop Type property set to true. That actually seems to work but I'm not sure if not using the COM references might have any downsides. Does it?

You need to copy interop assemblies (Microsoft Word/Excel/PowerPoint/Core 14.0 Object Libraries) to a local folder in the solution. And then you can add them as references, so they will be accessible on both machines and you will be able to open the project seamlessly between the two computers.

Related

Distribution of an application that requires Microsoft.Office.Interop.Excel

I have a c# (VS 2015) application that references Microsoft.Office.Interop.Excel
My code includes oXL = new Microsoft.Office.Interop.Excel.Application(); as well as the above reference.
If I compile and install that application on a machine that already has both Office 2013 and .Net 4.0 but when I search that computer I cannot find Microsoft.Office.Interop.Excel.dll on it anywhere which, I assume, is what I need.
I saw here:
By default, PIA’s are embedded in your solution when you build it so you don’t have to distribute PIAs to users as a prerequisite to using your VSTO Add-in or customization.
When I build my application I don't get Microsoft.Office.Interop.Excel.dll included in my Release folder (like I do with other referenced .dlls).
Do I need to add that manually or is the only way to get this installed to also install the Office Primary Interop Assemblies Redistributable from Microsoft?
Go to the properties of the reference and set copylocal to true.
Interop libs have them set to false by default.
This will copy the DLL, when you build the project.
The Copy Local property (corresponding to CopyLocal) determines
whether a reference is copied to the local bin path.
At run time, a
reference must be located in either the Global Assembly Cache (GAC) or
the output path of the project. If this property is set to true, the
reference is copied to the output path of the project at run time.
For a more in depth look into copy local see msdn
Jason, as far I know, you can select the version and Office package required to install your application.
If you are using Visual Studio, you can go to Settings (of your application) and insert the dependecies.
Check those links:
Deployment and Dependencies
How to: Create and Remove Project Dependencies
Jason, you have also to REFERENCE the MICROSOFT OFFICE XX.X OBJECT LIBRARY.
The XX.X is the release you have there.
When your project runs at client, VS will seach in GAC_MSIL to see if he/she has Office there.
Do not try to add EXCEL libraries because it won´t function. Your user must have Office to your code run.
The .NET application will look for the dll in the global assemblies and bind to whatever appropriate dll there is dynamically.
DLL = dynamically linked library
There are different dlls for dofferent versions of office and on different platforms (32 bit versus 64 bit).
In short, do NOT include the dll in your package.
Catch exceptions in case office is not installed.

c# - Office 2016 interop dlls [duplicate]

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.
Can I get it to work with office 2013 and be backward compatible?
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.
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.
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.
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!
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.
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.

Cannot add Excel to Project as a COM object

I am needing to create an Excel spreadsheet from a c# project, but I cannot add the reference to the Excel COM object.
I am using Visual Studio 2010 and office 2013. I've clicked on the References folder and selected Add Reference, and then navigated to the COM tab, setting them in alphabetical order. I looked through, but there was no reference to Microsoft.Office.Interop.Excel.
Trying to resolve the issue, I downloaded and installed the PIAs for Office 2010, but references still were not added. I'm not sure where these files would be located on the hard-drive, so I cannot just browse to them (or copy one to the project Resource folder and reference it there).
Does anyone have an idea as to why I cannot see the references?
Additional Information
I managed to get the Microsoft.Office.Interop.dll file from a coworker for Office 2013, but I was not able to reference it from my project since I am working in .NET 3.5. My manager does not want me to update the project to .NET 4.0, so it looks like I will need to use the Office 2010 PIAs I installed. Unfortunately, installing them did not add them to the list, and I haven't been able to find the dll's to add them by browsing to them.
I doubt that it will make a difference, but my dev system is Windows 8.

Adding interop 12 to visual Studio 2012

I can reference either interop.outlook 14 or 15 in my code. I downloaded interop.outlook 12.0 and also installed it but I'm not able to select to reference it as a option. I am trying to build a dll to pull contacts from outlook. So instead of writting a dll for each version of office, I want to try write one for office 2007 aka interop 12.0 which probably should cover most scenarios that i would encounter for my app/dll.
My Question is why I can't add a reference of interop.outlook 12.0.
Go to the property of the added outlook Interop reference and set the Embed Interop Type to False

Microsoft.Office.Core Reference Missing

Using the example provided in codeproject I am struggling to work out where I can find the reference to the library Microsoft.Office.Core.
I am getting the error "The referenced component 'Microsoft.Office.Core' could not be found."
I only have office 2007 enterprise edition and outlook 2003 installed on this system. Could this be the cause of this? Otherwise which specific dll am I supposed to be referencing?
You can add reference of Microsoft.Office.Core from COM components tab in the add reference window by adding reference of Microsoft Office 12.0 Object Library. The screen shot will shows what component you need.
None of the above answer helped me, i was using Visual Studio 2017. What I did is, installed Office/SharePoint Development using Visual Studio Installer.
After that, I was able to see 'office', this assembly contains Microsoft.Office.Core.
Hope this helps you.
You need to download and install the PIA (primary interop assemblies) for the version of Office you are using. Once installed you can then add a reference to your project and they will be available from the add reference dialog. Here are the links to download them...
Office 2010 PIA
Office 2007 PIA
Office 2003 PIA
After installing the Office PIA (primary interop assemblies), add a reference to your project -> its on the .NET tab - component name "Office"
If someone not have reference in .NET . COM (tab) or not have office installed on machine where visual was installed can do :
Download and install: Microsoft Office Developer Tools
Add references from:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Visual Studio Tools for Office\PIA\Office15
Now there is a nuget package for that.
https://www.nuget.org/packages/NetOffice.Core.Net40/
First I didn't find office in COM, so tried this nuget and it worked!
You can use this NuGet package which includes the interop assemblies in addition to the office assembly.
https://www.nuget.org/packages/Bundle.Microsoft.Office.Interop/
Have you actually gone to your references and added a .NET reference to the 'Microsoft.Office.Core' library? If you downloaded the example application, the answer would be yes. If that is the case, follow the advice in the article:
If your system does not have Microsoft Office Outlook 2003 you may have to change the References used by the "OutlookConnector" project. That is to say, if you received a build error described as "The type of namespace name 'Outlook' could not be found", you probably don't have Office 2003. Simply expand the project references, remove the afflicted items, and add the COM Library appropriate for your system. If someone has a dynamic way to handle this, I'd be curious to see you've done.
That should solve your problem. If not, let us know.
In case you are using Visual Studio 2012, for this to work and in order to make reference to Microsoft Office Core, you have to make the reference through Visual Studio by clicking on the top menu's Project, Add Reference, Extensions button and checking office which is now (14.0).
If you are not able to find PIA for Office 2013 then follow these steps:
Click on Solution Explorer in Visual Studio
Right click on your project name (not solution name)
Select 'Manage Nuget packages'
Click on Browse and search for PIA 2013, choose the shown PIA and click on Install.....
And you are done.
I have the same trouble. I went to Add references, COM tab, an select Microsoft Office 15.0 Objetct Library. Ok, and my problem ends.
part of my code is:
EXCEL.Range rango;
rango = (EXCEL.Range)HojadetrabajoExcel.get_Range("AE13", "AK23");
rango.Select();
// EXCEL.Pictures Lafoto = (EXCEL.Pictures).HojadetrabajoExcel.Pictures(System.Reflection.Missing.Value);
EXCEL.Pictures Lafoto = HojadetrabajoExcel.Pictures(System.Reflection.Missing.Value);
HojadetrabajoExcel.Shapes.AddPicture(#"D:\GENETICA HUMANA\Reportes\imagenes\" + Variables.nombreimagen,
Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue,
float.Parse(rango.Left.ToString()),float.Parse(rango.Top.ToString()), float.Parse(rango.Width.ToString()),
float.Parse(rango.Height.ToString()));
I faced the same problem when i tried to open my old c# project into visual studio 2017 version. This problem arises typically when you try to open a project that you made with previous version of VS and open it with latest version.
what i did is,i opened my project and delete the reference from my project,then added Microsoft outlook 12.0 object library and Microsoft office 12.0 object libraryMicrosoft outlook 12.0 object library
In my case when I added "Microsoft Excel Object Library" and "Microsoft Office Object Library" from Reference->COM then the reference error goes away.

Categories