1

We are working on file conversions in which I am trying to convert Word document (doc & docx) to PDF file using Microsoft Interop library.

Below is the code snippet I've tried implementing.

*public string Get()
        {
            try
            {
                Microsoft.Office.Interop.Word.Application appWord = new 
                Microsoft.Office.Interop.Word.Application();

                wordDocument = appWord.Documents.Open(@"<Document Path>");
                wordDocument.ExportAsFixedFormat(@"<Document Path>", 
                                                 WdExportFormat.wdExportFormatPDF);
               return "converted";
            }
            catch (Exception ex) 
            {
                string error = ex.Message.ToString();
                return error;
            }         
        }
        public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

But when I am using Rest API to convert Word file to PDF the above code snippet is giving the below error.

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)).

Here is some more information about which environment I'm working in:

Operating System : Microsoft Windows 10 Pro

Code Editor : Visual Studio 2022

Technology : C#(.Net Framework 4.6.2)

MS Office Version :Microsoft Office 365(32 bit)  

Note : We have to use Interop library only, no third party DLL can be used.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Where and when do you run the code? Is it run from a web application or service? – Eugene Astafiev Nov 08 '22 at 10:17
  • `when I am using Rest API to convert Word file to PDF` don't do that. First of all, you'd need to buy a Word license for every single user, anonymous or not. Then you'd need to install Word in a way that makes it accessible to the web app account. Even if that's a virtual account. Then you'd have to ensure you don't leak Word instances, which you do. That `Get` will start a new Word instance until the server runs out of RAM – Panagiotis Kanavos Nov 08 '22 at 10:27
  • The XML-based OpenXML formats were created *16 years* ago to get rid of the requirement of installing Office to read or generate Word or Excel files. What you ask isn't a file conversion though. PDF isn't a document format, it's a print format. It contains print instructions (PostScript specifically), not text and paragraphs. You can use the OpenXML SDK or a NuGet package to read a `docx` document but the real problem is *printing* that document to PDF. – Panagiotis Kanavos Nov 08 '22 at 10:30

1 Answers1

0

This is a widely spread issue when dealing with Office automation from a service or web applications. 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.

As a possible workaround you may consider using the Open XML SDK (see Welcome to the Open XML SDK 2.5 for Office) or any third-party components designed for the server side execution.

Also you may find a similar thread Accessing Office Word object model through asp.net results in "failed due to the following error: 80070005 Access is denied." helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • It's not just about instability. First of all, you'd need one Office license for every single anonymous user. Then you'd have to install Word on the machine, start it and ensure it shuts down correctly after every request. That's where all Office Interop code fails typically, leaking Word instances until the server crashes. Even if that works, you'd have to ensure only a few clients can start Word at a time to avoid flooding the server. – Panagiotis Kanavos Nov 08 '22 at 10:24
  • The issue is not related to the server crash. – Eugene Astafiev Nov 08 '22 at 10:53
  • We are not allowed to use third party library like openXML etc so is there any other alternative to do the same using Microsoft Interop library in Rest API, if possible please share ,code snippet or an article ,that would be really appreciated – Ruby Sharma Nov 09 '22 at 08:15
  • There is no other way to get the job done. – Eugene Astafiev Nov 09 '22 at 09:59