[Solved] Set default template

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
struhadlo
Posts: 3
Joined: Sun Oct 28, 2012 3:49 pm

[Solved] Set default template

Post by struhadlo »

Hello,

I have my delphi application which communicates with OpenOffice (application serves like a document manager). I did my template with macros which servers an event like a new document, document is closed etc. I use to call LoadComponentFromUrl with AsTemplate parameter from my application to open a new document with my template. It works fine. But I need to set my template as default template for OpenOffice Writer. I know how to do manually (File > Template > Organize), but don't know how to do from my application. I looked for some solution, but i didn't find anything. Could anybody give me any advice? Thanks
Last edited by struhadlo on Tue Nov 06, 2012 6:50 pm, edited 2 times in total.
Linux Mint 11, OpenOffice 3.4.1
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Set default template

Post by Charlie Young »

struhadlo wrote:Hello,

I have my delphi application which communicates with OpenOffice (application serves like a document manager). I did my template with macros which servers an event like a new document, document is closed etc. I use to call LoadComponentFromUrl with AsTemplate parameter from my application to open a new document with my template. It works fine. But I need to set my template as default template for OpenOffice Writer. I know how to do manually (File > Template > Organize), but don't know how to do from my application. I looked for some solution, but i didn't find anything. Could anybody give me any advice? Thanks
I tried this experiment, and it seemed to work, though I wouldn't be surprised if there is a more direct solution.

Before trying it yourself, I strongly recommend backing up your user profile.

Say the name of your Writer template file is MyTemplate.ott
First, and I assume you can do this programmatically, copy MyTemplate.ott into the template folder of your user profile

Code: Select all

....\OpenOffice.org\3\user\template\
The following is in Basic, so it's up to you to translate to Delphi (I could also give you c++, Python, or JavaScript, but my Delphi is lacking and my Pascal is very rusty). As you can see, we have MyTemplate.ott assigned to DefaultTemplateName.

Since this changes the Configuration data, it is normally necessary to restart Office before the changes take effect.

Code: Select all

Sub SetDefaultWriterTemplate()
       Dim sNodePath As String
       Dim oCP, oCUA
       Dim TextFactory
       Dim DefaultTemplateName As String
       
       DefaultTemplateName = "MyTemplate.ott"
       sNodePath = "/org.openoffice.Setup/Office/Factories"  
       
       Dim aProps(0) As New com.sun.star.beans.PropertyValue

       oCP = CreateUnoService("com.sun.star.configuration.ConfigurationProvider" )
       aProps(0).Name = "nodepath"
       aProps(0).Value = sNodePath
       oCUA = oCP.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", aProps )
       
       TextFactory = oCua.getByName("com.sun.star.text.TextDocument")
       TextFactory.ooSetupFactoryTemplateFile = "$(user)/template/" & DefaultTemplateName
       TextFactory.ooSetupFactorySystemDefaultTemplateChanged = True
       oCUA.commitChanges()
End Sub
If anything goes flaky, restore your profile and come back here and complain, but try at your own risk.
Apache OpenOffice 4.1.1
Windows XP
struhadlo
Posts: 3
Joined: Sun Oct 28, 2012 3:49 pm

Re: Set default template

Post by struhadlo »

It works!! Thank you very much.
Linux Mint 11, OpenOffice 3.4.1
Post Reply