Microsoft.Office.Interop.Word.Document.set_AttachedTemplate() locks template file

Nicolas Krzywinski 1 Reputation point
2022-05-10T13:00:53.593+00:00

Our c# UI application is using Microsoft.Office.Interop heavily to integrate Microsoft Office products visually.
We switched from the legacy document formats to the new OOXML document formats.

Now the behaviour for Microsoft.Office.Interop.Word.Document.set_AttachedTemplate() seems to have changed:
it locks the template file

Previously this wasn't the case. In our application, multiple documents get opened, the second document gets the locked file warning now.
How to prevent this?

----------

I traced the filesystem operations using Process Monitor and I can see a difference at the end: dotx has no CloseFile operation at the end, while dot has

dot template gets closed:
200627-globedot-gets-closed-old-sidoun-globe.png

dotx template doesn't get closed:
globedotx-no-close-current-sidoun-globe.png

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.
10,627 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nicolas Krzywinski 1 Reputation point
    2022-05-10T14:28:22.66+00:00

    We solved this using a workaround:

    1. Open the file with Aspose.Words
    2. Set AttachedTemplate
    3. Save and close the file
    4. Open the file with Microsoft.Office.Interop.Word

    It isn't pretty, but it works.

    0 comments No comments