Office 2016 automation - c#

I have an legacy Excel project that automates Excel 2007 on our server in order to produce overnight user-authored reports. It has worked fine but recently on trying to upgrade to Office 365 (also tried 2016) I came across a problem where it appears to throw COMException 0x800AC472 after creating the Excel.Application object.
I can create the object without error but as soon as I try to call any methods of the Application object it faults.
This is a brand new vanilla server with .Net 4.6 and 3.5 installed and Office 365, nothing else.
The code has been re-linked using COM to reference Office 15.0 Interop. And the same code still works perfectly on the old 2007 server.
Microsoft.Office.Core 16.0
Microsoft.Office.Interop.Excel 16.0
The Error message in full:
System.Runtime.InteropServices.COMException (0x800AC472): Exception from HRESULT: 0x800AC472
at Microsoft.Office.Interop.Excel.ApplicationClass.set_DisplayAlerts(Boolean RHS)
at ExcelReportingService.ExcelJob.ProcessExcelBook(String location, String savePath, String testEmailsOnlyTo, Boolean allowEmptyResultFile, StringBuilder& processResults) in W:\\Group\\VIP\\Services\\ExcelReporting\\Classes\\ExcelJob.cs:line 136
The code has never had an install package and I'm not sure if this is this is the issue? Currently I am just copying the DLLs for the app to the service folder and installing with "sc create". I have not installed any other runtimes or copied any DLLs into the GAC? But I don't do this with the working 2007 server either?
Am I missing a runtime I need to install on the server?
PS: I do recognize the reality that Microsoft has said that server automation is not supported for Office, but this is a legacy project that I need to get working as all servers are being upgraded and Office 2007 will not install on newer servers.

Related

Click once deployment - deployed application runtime error

I used the deploy feature from VS community 2017 for a custom level excel workbook 2010. The installation package got created on the client computer.
I could run the setup and checked that the workbook was installed.
At run time I got hang on an exception which I figured means that VSTO runtime library is not properly set.
Here is the error message I got (this is french sorry about that - word to word it says "Element not found":
System.Runtime.InteropServices.COMException (0x80070490): Élément introuvable. (Exception de HRESULT : 0x80070490)
à Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemProvider.GetHostExtender(String extenderType, String extenderCookie, IntPtr& extenderObject)
The workbook has a button layed on the surface of a sheet on some code C#.NET attached to the document.
It works and run on the development host not on the client host. Both are windows 10 OS.
The deployed components are :
.NET Framework 4.5.2
VS 2010 for Office Runtime
checking on the client computer I see .NET 4.6.* and VS 2010 for Office Runtime installed.
Theoretically .NET.4.6.* is backward compatible .NET 4.5.2.
Digging deeper I think something is wrong with VSTO run time. I ve read that the VSTO system runtime includes libraries used for the Microsoft.VisualStudio.Tools namespace. What's weird is that there is the same interface but in another namespace, Microsoft.Office.Tools, which is hardly not document ("This API supports the product infrastructure and is not intended to be used directly from your code").
I also have checked that the PIA is installed for Office and Excel.
I had VS 2017 installed on that computer afterward (after trying to run the deployed workbook).
I used the exact same code, built the solution and got the same exception !
Any clue on what to check ?

Windows Form + Excel Worksheet

I've built a windows application that retrieves information from an Excel Worksheet. I've tested and had it working on a computer with Microsoft Office Package installed.
I have a little problem now, I will be deploying this program to multiple workstations but they have OpenOffice installed. My issue is, do these workstations need have Microsoft Office installed or would it work regardless?
Thank you all.
If you're referencing the MS Office object library, then they need to have it installed.
Edit: You can probably install Open Office on your machine, write a similar call for that object library, and have it choose based on the installed program(s).

Can VSTO Addins be built without installing MS Office?

I am working on setting up build server for a VSTO Addin project. I am using Jenkins for the same. When I try to build project locally on my machine using MS Build it is building fine. However on server, it gives following error:
Error 10 Cannot find the interop type that matches the embedded interop type 'Microsoft.Office.Core.IRibbonExtensibility'. Are you missing an assembly reference?
I think this should be coming from PIA. Can I install PIA without MS Office installed on machine?
So, my question is it not possible to build VSTO solution without installing the MS Office suite on server?
I think this should be coming from PIA. Can I install PIA without MS Office installed on machine?
Yes, you are free to install PIAs without MS Office. It will allow to build the project, but at runtime you will get exceptions in the code. PIAs are used for marshalling your property and method calls to the unmanaged code, i.e. COM servers represented by Office applications.
is it not possible to build VSTO solution without installing the MS Office suite on server?
You are free to build VSTO add-ins on a machine without MS Office installed.

Excel interop works on machine with Office 2007 but fails on machine with Office 2010

I have a C# WPF (.NET 4.0) application that uses Excel interop to read data from an Excel file.
When I run this app on my development machine, which has Excel 2007, it works fine. When I run it on another machine that has Excel 2010 installed on it, it fails with the following error message:
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)).
The failure occurs when attempting to create a Microsoft.Office.Interop.Excel.Application object, as in:
var app = new Application();
My project has a reference to Microsoft.Office.Interop.Excel.dll (version 14.0.0.0, runtime version v2.0.50727), which is located at:
C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll
I tried to run the app with the default settings on the referenced dll:
Copy Local = False
Embed Interop Types = True
Specific Version = True
I also tried to run the app with the following settings on the referenced dll:
Copy Local = True
Embed Interop Types = False
Specific Version = True
But on the machine with Office 2010, neither of these attempts worked.
Other notes: The machine with Office 2010 is 64-bit w/ Windows 7. My development machine is X86 with Windows XP.
Question
What is causing the "class not registered" exception and how do I fix it?
Edit
In response to Alex's answer, I tried building the app as Any CPU, X86, and X64, but it made no difference. I'm still getting the same "class not registered" error.
Edit 2
Just tried the app on a 32-bit machine with Office 2010. No errors. So, it could be something wrong with the particular machine, or it could be the combination of 64-bit and Office 2010.
Edit 3
Okay, so now I've tested it on a different 64-bit machine with Office 2010. Again no errors. I think this means something is wrong with the particular machine and it's not my software's fault. Ahh programming.
Preliminary answer: my software was fine and something was wrong with the setup on that particular machine.
Edit
I just found out something that is probably the source of the problem. The problem machine has Office, but it is set up as click-to-run (runs off the internet) rather than MSI-based (runs from an EXE). This means that certain registry settings, DLLs, components, etc. are not available to Microsoft.Office.Interop.Excel, so it fails.
Edit 2
That was definitely the problem. Just upgraded to a full MSI-based installation of Office, and everything works great.
Edit 3
Added this as a bug to Microsoft Connect:
https://connect.microsoft.com/VisualStudio/feedback/details/672276/excel-interop-fails-on-machine-where-office-is-installed-as-click-to-run#details

Error when export Excel using Microsoft.Office.Interop.Excel

I use Microsoft.Office.Interop.Excel.dll COM to export excel. I distribute my application to clients. It runs well on my computer but on another computer, it cause a error.
I wonder that application package should be included Microsoft.Office.Interop.Excel.dll and the computer must be installed Microsoft Excel. Thanks.
P/S: My application is C# .net 2.0.
UPDATE: I am having a problem. I don't know the excel's version on the client computer. So, which version of PIA I will use?
If I build my application with Microsoft.Office.Interop.Excel.dll version 11 (suitable with Office 2003), but what problem will happen if the client computer have Office 2007. It will run or no? How to use only one Microsoft.Office.Interop.Excel.dll which run well on any client computers which have one of office 2003, 2007 or 2010.
Thanks.
Did you deploy the other necessary DLL's?
Interop.Microsoft.Office.Core.dll
Microsoft.Vbe.Interop.dll
EDIT:
You should see two references in your project: Microsoft.Office.Core and VBIDE. Click on these references and set the property "Local Copy" to true.
A complete refrence to installing and deploying the PIA can be found here: http://msdn.microsoft.com/en-us/library/aa159923%28office.11%29.aspx
On that website is also a link to a redistributable file you can include and run from your installer to install the PIA into the GAC.
If you want to use Office Automation you should always have Office installed. That's because the interop classes will create an instance of excel in the background and run all your actions on that instance.
If there is no office installed, it will thus not run properly. Please also keep in mind that using office automation on a server isn't the way to go (due to security issues and so on).
Are they using the same version of Excel? you could look into the PIA (Primary Interop Assembiles) to deploy with your app.

Categories