Manually opened Word files are being closed when files are opened using Microsoft.Office.Interop.Word.dll (15.0.0.0). How can I prevent this?

Shintaro Yoshida 0 Reputation points
2024-10-04T00:25:07.69+00:00

I have a C# program that uses Microsoft.Office.Interop.Word.dll (15.0.0.0) to repeatedly open Word files, gather information, and close them. However, while my application is running in the background, if I try to manually open a Word file from Explorer or another method, the file opens briefly but then closes unexpectedly.

Does anyone have a solution for this issue?

Conditions:

  1. The self-made C# application uses Microsoft.Office.Interop.Word.dll (15.0.0.0) to open Word files.
  2. I attempt to manually open a Word file using Explorer (or similar methods).
  3. Closing the Word file opened by the application also causes the manually opened file to close.

If the Word file is opened manually before the C# application starts, the manually opened file is not closed, as it seems to be attached to the first Word process that was already running.

Sample source

public override IEnumerable<LinkInfo> GetLinkInfo(string filePath)
{
    Application application = null;
    Document document = null;
    try
    {
        try
        {
            application = new Application
            {
                Visible = false,
                DisplayAlerts = WdAlertLevel.wdAlertsNone,
                AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable
            };
            document = application.Documents.Open(
                FileName: filePath,
                ReadOnly: true,
                PasswordDocument:"DummyPassword",
                PasswordTemplate: "DummyPassword");
        }
        catch (Exception ex)
        {
            throw ex;
        }

        foreach (Hyperlink hyperlink in document.Hyperlinks)
        {
            if (string.IsNullOrEmpty(hyperlink.Address))
            {
                continue;
            }

            string text = string.Empty;
            Range range = null;
            if (hyperlink.Type == MsoHyperlinkType.msoHyperlinkRange)
            {
                text = hyperlink.TextToDisplay;
                range = hyperlink.Range;
            }
            else
            {
                text = SHAPE_TEXT;
                range = hyperlink.Shape.Anchor;
            }
            int pageNumber = range.Information[WdInformation.wdActiveEndPageNumber];
            yield return new LinkInfo(hyperlink.Address, text, "P." + pageNumber.ToString());
        }
    }
    finally
    {
        document?.Close(false);
        application?.Quit();
        if (document != null)
        {
            Marshal.ReleaseComObject(document);
        }
        if (application != null)
        {
            Marshal.ReleaseComObject(application);
        }
    }
}

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
5,197 questions
Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
890 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,054 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,024 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
936 questions
0 comments No comments
{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.