User:Ptoye
Biog: Ex-IT consultant in data communications. Programming background. Wishes that the OO API documentation wasn't so difficult to understand and navigate.
Now a classical pianist.
Lives in Reading, England. Ptoye (talk) 10:52, 21 January 2013 (UTC)
Test area for new text
Opening a new file from a template
If you want to open a file using an existing template, use the template name instead of the file name. An untitled document will be opened from the template.
If the template location is known, simply use it. However, OpenOffice keeps at least two sets of templates: global templates available to all users and "My Templates" available to the current user, and the locations of the directories are not obviously available. They can be found by means of the PathSettings service which allows the user to examine the various directory paths known to OpenOffice. The Template property of this service returns a string which is a semi-colon separated list of URLS, each of which represents a possible location for the desired template. It is necessary to search this string and the resulting directories to find the template. The following code finds a user template and opens a document based on it:
Dim PathService as Object Dim TemplatePath as String ' The path list Dim SCPos as Integer ' The position of the next semi-colon Dim MyTemplatePath as String ' The URL for the user template ' substitute your template name here const TemplateName = "MyTemplate.ott" ' This seems to be the place in which OO stores user templates const UserTemplateDirectory="/user/template" PathService=CreateUNOService("com.sun.star.util.PathSettings") TemplatePath=PathService.Template MyTemplatePath="" do while len(TemplatePath) >0 SCPos=InStr (TemplatePath,";") if SCPos>0 then MyTemplatePath=Left(TemplatePath, SCPos-1) else MyTemplatePath=TemplatePath end if ' NOTE: as well as file URLs there are some others which should be ignored if InStr(myTemplatePath,"file:///")=1 And _ Right(myTemplatePath,14) = UserTemplateDirectory then exit do end if TemplatePath=mid(TemplatePath, SCPos+1, len(TemplatePath)-SCPos) MyTemplatePath="" loop if MyTemplatePath<>"" then TemplatePath=ConverttoURL(TemplateName) MyTemplatePath=MyTemplatePath & "/" & TemplateName InvoiceDoc=StarDesktop.LoadComponentfromURL(MytemplatePath, "_blank", 0, Array()) else MsgBox "Cannot find the template directory" end if
This code does not check that the desired template actually exists; if it doesn't, the LoadComponentfromURL call will fail.