Word Interop Issue with ASP.NET - c#

About Microsoft.Office.Interop.Word.
I installed the PIA and Microsoft Office 2010 in my web server, but I can't generate a .doc when I run the application published in my web server. When I run my application in localhost, works fine, and generate the .doc file.
I have to do some configuration in server side to allow the generation of .doc files ?

Remember that Office 2010 was not designed to run inside a web server (e.g., thread safety might not be guaranteed), so you might encounter more difficulties down the line.
Instead, consider using the Open SDK 2.0 from Microsoft, which allows you to manipulate (create, edit) Office 2010 documents, which are simply packaged (zipped) XML files. This technology is much better suited for server use. It also doesn't require you to have a separate Office 2010 license for each web server where you are going to install Office 2010.

You should be using the Open XML SDK for this. The office interops are outdated relics, and should not be used.
Download link:
http://www.microsoft.com/download/en/details.aspx?id=5124
Here's a simple example of how to create a Word document:
public void HelloWorld(string docName)
{
// Create a Wordprocessing document.
using (WordprocessingDocument package = WordprocessingDocument.Create(docName, WordprocessingDocumentType.Document))
{
// Add a new main document part.
package.AddMainDocumentPart();
// Create the Document DOM.
package.MainDocumentPart.Document =
new Document(
new Body(
new Paragraph(
new Run(
new Text("Hello World!")))));
// Save changes to the main document part.
package.MainDocumentPart.Document.Save();
}
}
See this MSDN article for more details:
http://msdn.microsoft.com/en-us/library/dd440953%28v=office.12%29.aspx

Office Interop is NOT supported by MS in "server-like scenarios" (which IIS/ASP.NET is a special kind of)... see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
Your options include several libtraries (free and commercial) - for example:
MS OpenXML SDK (free)
Aspose.Words (commercial)
Regarding the exception you get:
Since Windows Vista several changes have been introduced to stop any Windows Service (IIS is just some special one) from doing anything "desktop-like" - this is due to security issues... to resolve such a situation you would need to circumvent those security measures implemented by MS - which I absolutely do NOT recommend...

Related

How to Configure Microsoft.office.interop to server's Microsoft Office..?

i have to convert a word doc into pdf and print it and Microsoft.Office.interop offers an easy way to do it but client machine has be installed with 2013 or higher.
My question is. is there any way that i could convert the word file into pdf using Server's Microsoft office because i can't expect client have 2013 installed on their pc
if no is there any easy and free way to convert word file into pdf.?
Thanks all
Word Automation Services (part of SharePoint) can convert Word documents to PDF (and other file formats) in the server environment.
It is not recommended to use Office Interop on server.
You can use OpenXML for working with office docs.
There are many examples available here.
For conversion, you will have to use separate plugin, and I'm not aware of a free version to convert to PDF.

Using Office.Interop.Excel on machines where MS Office isn't installed

I have an application that uses Microsoft.Office.Interop.Excel, when I deploy it on machines where there isn't any version of MS Office I get the following error
I tried installing VSTOR as indicated in this so answer COM object with CLSID {00024500-0000-0000-C000-000000000046} is either not valid or not registered
but this didn't solve the issue.
If you need to work only with open XML documents (*.xslx) you may consider using the Open XML SDK. See Welcome to the Open XML SDK 2.5 for Office for more information.
In case of binary file format you need to use any third-party components that don't required Office/Excel installed on the machine.
Pay special attention to the following fact:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
Read more about that in the Considerations for server-side Automation of Office article.
You can't use Microsoft Interop libraries if MS Office is not installed. You might want to look at alternative options to read Excel or Word files. There are many free and (paid) C# libraries you could use for this purpose, for example:
https://exceldatareader.codeplex.com/
https://github.com/ExcelDataReader/ExcelDataReader
You need download and install Microsoft Office {year}: Primary Interop Assemblies - it's free.

Is it required to install MS Excel or Office on server to read an excel file in web application?

I'm working to create and read MS Excel file in asp.net web application.
I'm not sure that it requires to install Microsoft Excel on server or not.
I don't want to install any licensed product on the server like MS office etc.
Please let me know how i can implement this functionality without installing MS Excel on the server or it is necessary to install MS excel on server?
Thanks
It depends on what you're doing, but more than likely, all you need is the Microsoft Access Database Engine.
This download will install a set of components that facilitate the
transfer of data between existing Microsoft Office files such as
Microsoft Office Access 2010 (*.mdb and .accdb) files and Microsoft
Office Excel 2010 (.xls, *.xlsx, and *.xlsb) files to other data
sources such as Microsoft SQL Server. Connectivity to existing text
files is also supported. ODBC and OLEDB drivers are installed for
application developers to use in developing their applications with
connectivity to Office file formats.
Edit, in response to
I just want to read and write MS excel files but i don't want to install anything on the server.
You can try EPPlus, which seems to solve your problem; though I've never tried it.
EPPlus is a .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx).
No it isn't necessary to install a full version of Microsoft Office. You can just use ODBC or OLE to open the files, provided you just need to do simple operations like read rows in worksheets (as if they were tables).
Use http://www.connectionstrings.com/ to look up the various .NET ways of accessing Excel using ODBC and OLE.
Effectively you need to treat the .xls and .xslx files as if you were querying an SQL database.

How to create an Excel application using PIA (2010, version 14.0) and target machine doesn't have MS Office client installed on it

I'm facing one issue, actually I need to create one application in C# using VS2012, .Net framework 4.5 which reads excel and do some operation, but the issue is target machine doesn't have MS Office client installed on it.
And one restriction is I can't use the other open assemblies like Open XML etc. I have to only use the 2010 PIA.
It'll be great if someone can explain me how can implement this?
This can not be possible without installing MS Office on server. To use PIA you have to install the MS Office on server. Otherwise, if you doesn't want to install MS Office on server, it's better to use Open XML.

How to register a Visio class on a machine where it is not installed?

I have a task to create empty Visio document file (.vsd) with single page in it and drop few shapes there. My problem is that I have to write Windows console application for this purpose and it must run in the server environment where Visio is not installed. Is it possible to do that?
If I run application on my development environment, new instance of Visio is started. But, on my server, where my application must run, Visio is not installed and Visio application launch will fail. What I can do in this situation?
Edit: Getting Visio installed on servers is not an option.
If you're not tied to binary, then I would use Visio's xml format - vdx. Saveen Reddy has a great library (including NuGet) that makes life very simple - see
VisioAutomation.VDX
Also, Visio 2013 now uses a new package (zip style) file format - see:
VSDX: the new Visio file format
10 tips for developers working with the Visio VSDX file format
All of the above will enable you to generate Visio documents without having the application installed.

Categories

Resources