How to convert a word document to pdf document using Microsoft Interop library

4k Views Asked by At

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.

1

There are 1 best solutions below

4
On

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.