Detach / reAttach the template

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Lupp
Volunteer
Posts: 3554
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Detach / reAttach the template

Post by Lupp »

Just read and considered viewtopic.php?f=29&t=106238.

Manual proceeding may be a bit ... unhandy.

Therefore the following pieces of code:

Code: Select all

REM Raw! Not very thoroughly analyzed and only superficially tested!
Sub detachTemplate()
memorize  = True
uPropName = "memorizedTemplate"
props     = ThisComponent.DocumentProperties
If memorize Then
  tn = props.TemplateName
  tu = props.TemplateURL
  uProps = props.UserDefinedProperties
  h = getPropertyValueIfExisting(uProps, uPropName)
  If IsNull(h) Then
    uProps.addProperty(uPropName,,tu)
  EndIf
EndIf 
props.TemplateName = ""
props.TemplateURL  =""
End Sub

Sub reAttachMemorizedTemplate()
uPropName = "memorizedTemplate"
props     = ThisComponent.DocumentProperties
uProps    = props.UserDefinedProperties
h = getPropertyValueIfExisting(uProps, uPropName)
If NOT IsNull(h) Then
  tURL = h
  tURLSpl = Split(tURL, "/")
  tName   = tUrlSpl(Ubound(tURLSpl))
  props.TemplateURL  = tURL
  props.TemplateName = tName
REM To avoid conflicts iof later a different template may be attached:
  uProps.removeProperty(uPropName)
EndIf
End Sub

Function getPropertyValueIfExisting(pPropertyContainer As Object, pName As String)
getPropertyValueIfExisting = Nothing
On Local Error Goto fail
getPropertyValueIfExisting = pPropertyContainer.getPropertyValue(pName)
fail:
End Function
If you edit/create the Custom Property "memorizedTemplate" in advance, you can also use the Sub reAttachMemorizedTemplate() to attach a different template.
With one example everything worked in LibreOffice V 7.2 and also in AOO V 4.1.

The extension "Template Changer" was discussed more than once in this forum. It seems to be still maintained for LibO. (I never used it ...)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply