We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Attaching new template to document

Hi there,

I have a problem setting a new template to my document.
I have a word template “Template1.dot” (header image on the right side, red text) and a word template “Template2.dot” (header image on the left side, green text). Additional, I have an empty word document “EmptyDoc.doc”. There is nothing special about all these files (I also tried the same files with dotx and docx extension).

When I open the document using Aspose and try to set the AttachedTemplate property and save it to a new word document “Output.doc”, the output keeps blank.
Also, when I try to open the document with Tempate1 and change the AttachedTemplate property to Template2, the first attached template is kept.

Here is my sample code:

var template1 = @"C:\Users\mario.wegner\Desktop\AsposeTest\Template1.dot";
var template2 = @"C:\Users\mario.wegner\Desktop\AsposeTest\Template2.dot";
var emptyDoc = @"C:\Users\mario.wegner\Desktop\AsposeTest\EmptyDoc.doc";
var output = @"C:\Users\mario.wegner\Desktop\AsposeTest\Output.doc";

var doc = new Aspose.Words.Document(emptyDoc);
doc.AttachedTemplate = template1;
doc.Save(output);

//this doesn’t work, too 
//var doc = new Aspose.Words.Document(template1);
// doc.AttachedTemplate = template2;
// doc.Save(output);

My Aspose version is 16.1.0.0

I hope, someone can give me a hint, what I’m doing wrong.

Best regards,
Mario Wegner

Hi Mario,

Thanks for your inquiry.

Mario.Wegner:

When I open the document using Aspose and try to set the AttachedTemplate property and save it to a new word document “Output.doc”, the output keeps blank.

Please note that Aspose.Words mimics the same behavior as MS Word does. You are facing the expected behavior of Aspose.Words. If you open the EmptyDoc.doc in MS Word and attach template document (Template1.dot) to it, you will get the same output. We suggest you please read following web links for your reference.

What happens when I attach a new template to my document?

What is the relationship between a Microsoft Word document and its template?

Mario.Wegner:

Also, when I try to open the document with Tempate1 and change the AttachedTemplate property to Template2, the first attached template is kept.

You are importing Template1.dot into Aspose.Words DOM. In this case, the contents of document (header image on the right side and red text) will be available in Aspose.Words.Document.

Hope this answers your queries. Please let us know if you have any more queries.

Hallo Tahir,

indeed I was totally wrong about the connection between a document and its template. But the two links helped me to understand it correctly.

Many thanks for your helpful reply.

Best regards,
Mario Wegner