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

Ruby Sharma 1 Reputation point
2022-11-08T09:13:15.207+00:00

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.

Microsoft 365 and Office | Word | For business | Windows
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.