Share via

Microsoft.Office.Interop libraries throwing exception 0x80070005 (E_ACCESSDENIED)

Burra, Manish Kumar 0 Reputation points
2025-11-19T01:58:53.7066667+00:00

I am trying to Convert Word to PDF and Excel to PDF using Microsoft.Office.Interop.Excel (15.0.4795.1001) and Microsoft.Office.Interop.Word (15.0.4795.1004) nuget packages in .NET 8

console C# application.

I Deployed the app in windows servers and while try to run using service account below error is thrown. Kindly Assist.

Exception: Access is denied. (0x80070005 (E_ACCESSDENIED))     at DataProcess.Processes.Steps.FileConvert.ConvertWordToPDF(String inputFile, String outputFile) in \FileConvert.cs:line 96    

Code Snippet:

=========
var wordApp = new Microsoft.Office.Interop.Word.Application();

wordApp.Visible = false;

var document = wordApp.Documents.Open(inputFile);

document.ExportAsFixedFormat(outputFile, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);

document.Close(false);

wordApp.Quit();

Microsoft 365 and Office | Excel | Other | Windows
0 comments No comments

1 answer

Sort by: Most helpful
  1. Francisco Montilla 30,620 Reputation points Independent Advisor
    2025-11-19T03:12:18.97+00:00

    Hello,

    That's a limitation. Office COM automation is not supported under non-interactive contexts like Windows services on a server. In that scenario Word.Application and Excel.Application often fail with E_ACCESSDENIED even if you tweak DCOM and folder ACLs, and any workaround you get working will be brittle.

    The official way to do server-side DOCX/XLSX-to-PDF is to stop using Microsoft.Office.Interop and call Microsoft Graph to fetch a PDF rendition from OneDrive or SharePoint, which uses the Office for the web renderer.

    Put the file in OneDrive or a SharePoint document library, grant your app access (app-only via Entra ID with Sites.Selected or Files.Read.All as appropriate), and download the PDF stream from the Graph endpoint /drives/{driveId}/items/{itemId}/content?format=pdf.

    Here is a minimal .NET 8 sample using MSAL and HttpClient with client-credentials. It works for both Word and Excel files and writes the PDF to disk:

    using System.Net.Http.Headers;
    using Microsoft.Identity.Client;
    
    static async Task ConvertViaGraphAsync(
        string tenantId,
        string clientId,
        string clientSecret,
        string driveId,       // OneDrive or SharePoint drive ID
        string itemId,        // The file item ID in that drive
        string outputPdfPath) // Local path for the PDF
    {
        var app = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithClientSecret(clientSecret)
            .WithTenantId(tenantId)
            .Build();
    
        var token = await app
            .AcquireTokenForClient(new[] { "https://graph.microsoft.com/.default" })
            .ExecuteAsync()
            .ConfigureAwait(false);
    
        using var http = new HttpClient();
        var url = $"https://graph.microsoft.com/v1.0/drives/{driveId}/items/{itemId}/content?format=pdf";
        using var req = new HttpRequestMessage(HttpMethod.Get, url);
        req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
    
        using var res = await http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
        res.EnsureSuccessStatusCode();
    
        await using var fs = File.Create(outputPdfPath);
        await res.Content.CopyToAsync(fs).ConfigureAwait(false);
    }
    

    This avoids launching Office on the server and runs happily under a service account. You will need the file to live in OneDrive or SharePoint and the tenant must have licenses that include Office for the web so Graph can render the PDF.

    If you want, tell me where your files live today and I will map that to the exact driveId and itemId you should call.

    Was this answer helpful?

    0 comments No comments

Your answer

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