Ask Your Question
0

how to write macro to set page margins and orientation

asked 2019-03-03 13:25:41 +0200

Allen J gravatar image

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.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2019-03-03 13:42:26 +0200

Allen J gravatar image

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
edit flag offensive delete link more
1

answered 2019-03-03 15:40:09 +0200

Lupp gravatar image

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
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-03 13:25:41 +0200

Seen: 80 times

Last updated: Mar 03