Page 1 of 1

[Solved] Set default template

Posted: Tue Oct 30, 2012 11:35 pm
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

Re: Set default template

Posted: Sat Nov 03, 2012 3:40 pm
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.

Re: Set default template

Posted: Tue Nov 06, 2012 6:50 pm
by struhadlo
It works!! Thank you very much.