How to attach a template to a file


Is there a way to attach a template to a file (not the default template) so that every time that template gets updated, the styles in the document is updated when it is reopened?

Lemme explain.

I have two clients who have different setups for their documents. I created templates for both documents, but when I create a LibreOffice document, I can’t “attach” a template to the document. SO when there is an update (it happens) I have to remember to reload the styles in the document.

I know that you can do it in Word (using the Developer menu) and I was wondering if there was the same capability in Writer.

I searched but didn’t find it so either it doesn’t exist, or I’m not using the right terms to find my answer.



I don’t clearly understand what you mean by “attach a template to the document”.
Do you miss a StyleFamily once having loaded the styles from the respective template.
What else?
Templates aren’t attached to documents, and since they not only consist of style definitions, but also mostly have contents with styles applied, I hardly can imagine what this atttaching should do.
Documents are created from a template. If you do so, the documents will know their templates, and if the templates still are accessible, documents can update their styles when re-opened after the templates were reworked.
Having imported and applied the styles from a template via >File>New>Templates e.g. , You can create a new document from the template, clear the contents, and paste all the content from the other docuent into it. This way you get a document connected to the template.

It appears that once you have based a document on a template, the document forgets all about the template, and when you modify the template, you can’t update the changes in the document. A workaround for that is the template changer extension found in Template Changer » Extensions. It boils down to breaking the link with the template and then linking it to the same but modified, or another template. Note: standard text in the template file will not be inserted or modified in the document, just the styles definitions in the document get overridden by those in the template.

Why do you think so?

If the document was created with a template (not the hard-coded defaults), it will know the template (and show this in the DocumentProperties). If the template is not moved or deleted, the document will check when being opened if the template was changed, and you will be prompted concerning an update.

@Lupp: Why do I think so? Because in a test document that I made for the purpose, the update doesn’t happen. And the document properties dialog window doesn’t show the template name. So, there is a way to start a new document, based on a custom template, without Writer noticing it. I also read the information about the template changer extension, which seems to support my point of view.

Edit: I went for it again and found that when you use File - Templates - Save as Template, and then base a new document on it, it will store the template name. But when I then modify the template, the file doesn’t notice it.

Seems your LibO created the document with the internal hard-coded styles: No template in this case.
I actually tested once again everything I claimed. Of course, it doesn’t make much sense to attach an example, because your LibO hasn’t access to my templates. You may, however try the code I posted in my answer below.
An open file will not notice changes to its template. When re-opening a document it should check if there were changes to the template after the document’s creation. If yoiu deny, the connection may get broken. (No thorough research again.)

Thanks for the pointer, I will look at that.

Opening Documents With Templates

I went once more over the topic and verified my claims.
In addition I found that a document actually can be connected to a different template by a few lines of Basic code, and that it will also check the new one for changes then.
The proceeding surely comes with disadvantages and risks. I did not thoroughly research all the expectable implications, not to speak of the surprising ones.
The code below is posted explicitly emphasizing that applying it is sole responsibility of the user.
Sub changeOrSetTemplate
docProps = ThisComponent.DocumentProperties
docProps.TemplateURL = “file://FakePathToUserProfile/template/”
docProps.TemplateName = “NextTemplate”
End Sub

1 Like

Thanks for the code. I will try it out to see.

Is there a way to have this run automatically when you open a document? Then, maybe I can use the Template property that @floris_v mentions and have it do a check before loading. I’ll have to check.


I’m not experienced with every detail in the field. The assignment of a (new/different) template to a document may interfere with previously applied formatting in ways I currently don’t take in account. Therefore I would suggest to (wherever possible) create documents based on the appropriate templates. A later assignment should be done cautiously (with a copy probably). To automate such a process would clearly require much more complicated code than what I posted.

Concerning the statements by @floris_v I don’t agree, and I don’t know what “Template property” he mentioned and you want to “use”.
You may, however, add-on the “Template Changer” extension floris_v mentioned. Its author should have studied every aspect much more thoroughly than I did. On the other hand there seem to be flaws (read the page).
If you want my further assistance: I won’t write another “Changer Extension”. If you can describe exactly your use-case, and give an example of a document needing a template-assignment, and the template itself, I may be able to help to find a way to do it.