Attach Template DOTX to Word Document | Automatically Update Document Styles C# or VB.NET | Load Unload Template

Hi, I’d like to change the AttachedTemplate property for an existing word file, but I ran into an exception when saving the file. Here’s my code:

Dim wordDokument As New Aspose.Words.Document(existingFilePath)
wordDokument.AttachedTemplate = myNewFormatTemplatFilePath
wordDokument.AutomaticallyUpdateSyles = True
Dim ooxmlSaveOptions As New Aspose.Words.Saving.ooxmlSaveOptions(Aspose.Words.SaveFormat.Docx)
ooxmlSaveOptions.Compliance = Aspose.Words.Saving.ooxmlCompliance.Iso29500_2008_Transitional
wordDokument.Save(existingFilePath, ooxmlSaveOptions)

@BAV_Institut,

Please ZIP and attach the following resources here for testing:

  • Your simplified source Word document
  • The template Word DOTX file that you want to attach to source document
  • Please also create a standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information.

Hi and thanks for your reply. Here is a console project:
AsposeTemplateTest.zip (9.9 MB)

@BAV_Institut,

We also observe the following exception upon running your code with 15.1x versions of Aspose.Words for .NET API:

System.InvalidOperationException
  HResult=0x80131509
  Message=Collection was modified; enumeration operation may not execute.
  Source=mscorlib 

But, this problem does not occur when running your code with latest 21.2 version of Aspose.Words for .NET. So, we suggest you to please upgrade to the latest version of Aspose.Words for .NET.

Ok, thanks. :grinning: