Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I would prefer to create a template and to import the page style from it.
There was a very similar request a while ago, but I couldn't find it now. At that occasion I wrote the code below for the purpose. It worked well. The code contains a little helper supporting a lookup for the extension of the file in an array listing the allowed extensions. You may remove these oarts if not needed.

Sub loadOverwritePlainTextStyles(Optional p1)
REM Depending on the way the Sub is called there may be passed a parameter or not.
REM A complete analysis of the actual situation is not quite simple.
REM Here I treat p1 just as a dummy, and assume the Sub is called passing 1 or 0 parameters
REM and no relevant information is to extract from the parameter if any. 
acceptExt  = Array("txt", "csv", "text")
doc0       = ThisComponent
url        = doc0.URL
If url="" Then Exit Sub 
REM ThisComponent was not loaded from a file. 
REM A new document should be derived from the proper template.
urlSplit   = Split(url, ".")
urlSplitU  = Ubound(urlSplit)
ext        = urlSplit(urlSplitU)
If (getIndexByContent(acceptExt, ext)<0) OR _
   NOT doc0.SupportsService("com.sun.star.text.TextDocument") Then Exit Sub 
If IsMissing(p1) Then 
  REM See above!
End If
templateFN = "C:/Users/Somebody/AppData/Roaming/LibreOffice/4/user/template/justForTest.ott"
REM MUST BE REAPLACED BY THE CORRECT PATH OF THE TEMPLATE From WHICH TO IMPORT STYLES.
fr0        = doc0.CurrentController.Frame
dh         = CreateUnoService("com.sun.star.frame.DispatchHelper")
Dim _
loadPageStyleArgs(1) As New com.sun.star.beans.PropertyValue
loadPageStyleArgs(0).Name  = "FileName"
loadPageStyleArgs(0).Value = "file:///" & templateFN
loadPageStyleArgs(1).Name  = "Flags"
loadPageStyleArgs(1).Value = 21           REM Also page styles, Overwrite
dh.ExecuteDispatch(fr0, ".uno:LoadStyles", "", 0, loadPageStyleArgs())
End Sub

Function getIndexByContent(pArray, pContent)
getIndexByContent = -1
For k = 0 To Ubound(pArray)
  If pArray(k)=pContent Then
    getIndexByContent = k
    Exit For
  End If
Next k
End Function