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

Nicolas Krzywinski 1 Reputation point

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:

dotx template doesn't get closed:

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

    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