I need to frequently open a large number of .txt files and easily change the default page margins and orientation. A similar previous question was closed as irrelevant. The suggestion was to put a page style in the default template. This does not work for me since the .txt files are not opened with the default template. I have an answer and will post the code.
The following seems to work:
Sub SetPageMO ' margins and orientation Dim oViewCursor 'Current view cursor Dim oStyle 'Current page style oViewCursor = ThisComponent.CurrentController.getViewCursor() ' page style name Dim pageStyle as String pageStyle = oViewCursor.PageStyleName ' get page style object oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(pageStyle) ' set margins ' .5" = 1270 (.5 * 2540) oStyle.BottomMargin = 1270 oStyle.LeftMargin = 1270 oStyle.RightMargin = 1270 oStyle.TopMargin = 1270 ' set orientation oStyle.IsLandscape = True ' swap page dimensions temp = oStyle.Width oStyle.Width = oStyle.Height oStyle.Height = temp End Sub ' End SetPageMO
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
The mentioned topic I couldn’t find was Header and Footer needed outside the page margin - #10 by JohnJR .