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 and Office | Development | Other
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | Word | For business | Windows
Developer technologies | C#
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.