Programmatically open a word document in C#

Pieter Claassens 40 Reputation points
2026-01-15T10:32:30.1666667+00:00

Good day,

I am trying to programmatically open a word document in C#. However, I got this message when running the app:

System.IO.FileNotFoundException

 HResult=0x80070002

 Message=Could not load file or assembly 'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'.

Error: The system cannot find the file specified.   Source=<Cannot evaluate the exception source

I tried the following solutions online:

1.       https://stackoverflow.com/questions/32399420/could-not-load-file-or-assembly-office-version-15-0-0-0

2.       https://www.add-in-express.com/forum/read.php?FID=5&TID=15525  

3.       https://support.microsoft.com/da-dk/topic/june-18-2019-kb4503864-cumulative-update-for-net-framework-3-5-4-7-2-4-8-for-windows-10-version-1809-and-windows-server-2019-61112e18-6405-1abd-1ecd-e5b9b2177d40

4.       https://learn.microsoft.com/en-us/answers/questions/5100686/could-not-load-file-or-assembly-office-version-12

5.       https://support.microsoft.com/en-us/office/repair-an-office-application-7821d4b6-7c1d-4205-aa0e-a6b40c5bb88b?CorrelationId=1b0030f4-8cc5-4485-b495-810e2a7d1284&ui=en-US&rs=en-US&ad=US

I appreciate the help / assistance in advance.

 

Windows development | Windows App SDK
{count} votes

Answer recommended by moderator
  1. Michael Le (WICLOUD CORPORATION) 10,475 Reputation points Microsoft External Staff Moderator
    2026-01-16T04:00:04.52+00:00

    Hello @Pieter Claassens ,

    I'm glad you were able to resolve the issue. For anyone who might encounter a similar problem in the future, I'd like to provide some additional context.

    There are typically three common scenarios when opening Word documents:

    1. Simply viewing the document - letting the user open and interact with it normally
    2. Reading or modifying content programmatically - extracting or changing text, tables, etc. without showing the UI
    3. Automating Word with full UI access - using Word's specific features like macros, formatting tools, or displaying the application to users

    Since you're using Office Interop to open Word with visible UI, you've chosen option 3. However, there are some important considerations to keep in mind:

    • Every machine running your application must have Microsoft Office installed.
    • Microsoft explicitly doesn't support Office automation in server-side applications (ASP.NET, Windows Services, Azure Functions, etc.) due to security, stability, and performance concerns.
    • Without proper cleanup, Word processes can remain running in the background, causing memory leaks.

    You could use this code snippet as a reference:

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Runtime.InteropServices;
    
    Microsoft.Office.Interop.Word.Application wordApp = null;
    Document doc = null;
    
    try
    {
        wordApp = new Microsoft.Office.Interop.Word.Application();
        object filePath = @"C:\Test\NewDocument.docx";
        object readOnly = false;
        object isVisible = true;
        object missing = Type.Missing;
    
        doc = wordApp.Documents.Open(ref filePath, ref missing, ref readOnly, 
                                      ref missing, ref missing, ref missing, 
                                      ref missing, ref missing, ref missing, 
                                      ref missing, ref isVisible);
        wordApp.Visible = true;
    
        // Your work with the document here
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error opening document: {ex.Message}");
    }
    finally
    {
        // Cleanup COM objects
        if (doc != null)
        {
            doc.Close(false);
            Marshal.ReleaseComObject(doc);
        }
    
        if (wordApp != null)
        {
            wordApp.Quit(false);
            Marshal.ReleaseComObject(wordApp);
        }
    
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-01-15T10:32:42.9533333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To programmatically open a Word document in C#, you can use the Open method from the Documents collection in the Microsoft Office Interop library. Here's a basic example of how to do this:

    using Microsoft.Office.Interop.Word;
    
    // Create a new instance of Word application
    Application wordApp = new Application();
    
    // Open the document
    Document doc = wordApp.Documents.Open(@"C:\Path\To\YourDocument.docx");
    
    // Make the application visible (optional)
    wordApp.Visible = true;
    

    Make sure that the path to the document is correct and that the document exists at that location. The FileNotFoundException you are encountering suggests that the specified file path may be incorrect or that the Office Interop assemblies are not properly referenced in your project.

    To resolve the assembly loading issue, ensure that you have the correct version of the Office Primary Interop Assemblies (PIAs) installed and referenced in your project. You can check the references in your project and ensure that they point to the correct version of the Office assemblies that match the version of Office installed on your machine.

    If you're using Visual Studio, you can add the reference by:

    1. Right-click on your project in the Solution Explorer.
    2. Select "Add" > "Reference...".
    3. In the Assemblies section, look for Microsoft.Office.Interop.Word and add it.

    Also, ensure that your project is targeting the correct framework version that supports the Office Interop libraries.

    If the problem persists, consider repairing your Office installation or reinstalling the necessary components.

    1 person found this answer helpful.

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.