0

I have an ASP.NET 4.5 web forms app running on an IIS 7.5. I created a page where the user inputs some information and selects some stuff. At the bottom of the page I have a button that generates a word document.

How ?

I am using Microsoft Word Object Library and I have uploaded a word document template. In it I have added MergeFields which I am refering in the code behind and initializing them. This works great locally. When I uploaded the app on the server..big surprise:

Server Error in '/' Application. Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

The weird thing about it is that I have a few more files in the folder where I uploaded this word document template. Those are working fine inside the same application. I am using some excel templates. It's clearly a thing of not having access but why and how can I fix it ?

I tried to follow the instructions and right clicked on it to see the security properties. It already has the groups added and full rights assigned. Why can't it access it ? It might be good to mention here that I am using impersonation.

I would really like to know if there is a way to fix this or do I need to write all the word manipulation code using a different library ?

Thank you!

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Cosmos24Magic
  • 219
  • 4
  • 17
  • 2
    Using `Microsoft.Office.Interop` based automation of Microsoft Office (Word, Excel, PowerPoint, etc) in a server-side environment (like IIS) is not supported by Microsoft, and is also very prone to errors. Maybe have a look at alternative libraries like `OpenXML` (or other libraries based on it) which operate on the files directly instead of "automating" an Office Desktop Application. For more Informations have a look at the following support link from Microsoft: https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office – bassfader Sep 22 '17 at 12:28
  • Thank you for your answer. I see what you mean but I don't want to believe it. (kidding)..Why are there still documentations and tutorials about using interop in asp.net if it's not supported ? Just to waste people's time ? I'll have a look at OpenXML. It seems like what I need. I had the exact same problem with Excel and ended up using EPPlus instead of interop. – Cosmos24Magic Sep 22 '17 at 12:37
  • Possible duplicate of [OpenXML SDK 2.0 vs Aspose for server side word 2007 document generation in .NET](https://stackoverflow.com/questions/6302422/openxml-sdk-2-0-vs-aspose-for-server-side-word-2007-document-generation-in-net) – Lex Li Sep 22 '17 at 17:40
  • Microsoft never publishes documentation or tutorials about Office automation in ASP.NET, but the KB article. All the documentation or tutorials for the community is out of their reach, and you as the reader have the responsibility to filter out the truth from lies. – Lex Li Sep 22 '17 at 17:41
  • My thread is not like that "OpenXML vs Aspose". I'm not looking for a comparison and I'm not interested in Aspose. The main problem with OpenXML is that it's difficult to work with. I want to use it to replace merge fields from a document template and save it as a word document. Interop can very easily replace merge fields. I'm trying to swap to OpenXML right now but don't know how to replace those merge fields. Any help is welcome. Thank you! – Cosmos24Magic Sep 25 '17 at 10:58
  • 1
    Best thing is (IMHO) to look at different files using the *"Open XML SDK Productivity Tool"*, which is usually installed together with the OpenXML SDK. It can generate code from an OpenXML document, and it also provides the functionality for comparing the contents of 2 documents. So in your case I'd suggest to create a file with 1 merge-field placeholder, then create a copy of the file and fill in some text, and then finally compare both files using the OpenXML productivity tool to see what has changed (in the XML and/or the Code). – bassfader Sep 25 '17 at 13:47
  • Thank you! This really helped! I was able to see the structure and understand it better. I managed to replace the mergefields but now I'm struggling with bookmarks. In a word doc I have some multiline text, some bulleted lists, etc..and I have bookmarked it so that I would insert it in a merge field from the initial word template. This works fine in Interop and it's very easy to do. But in OpenXML not so much. So I used this tool and I noticed that inside every BookmarkStart there is a Run for each line. So I just have to loop through those runs to get the data. Is there a better way to do it? – Cosmos24Magic Sep 26 '17 at 05:20
  • @Cosmos24Magic Microsoft never supported server side Office automation. E.g. https://support.microsoft.com/en-us/help/257757/considerations-for-server-side-automation-of-office Developers can use Automation in Microsoft Office to build custom solutions that use the capabilities and the features that are built into the Office product. Although such programmatic development can be implemented on a client system with relative ease, a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET, DCOM, or a Windows NT service – user3285954 Nov 06 '17 at 13:45

0 Answers0