Page 1 of 1

[Solved] Line Spacing in Writer

Posted: Sun May 13, 2018 8:47 am
by Cogito ergo sum
Can someone please help with this?

Code: Select all

' =========================
' Line Spacing.vbs
' =========================

 Dim arg()

  Input1 = "I want this text written in larger Segoe Font characters and to be blue, " &_
  "and the line spacing to be normal single line. " 

  Input2 = "This text will be written in italic with the Cambria font and in a size usual " &_
  "for printed text, but with expanded (double) line spacing. The colour is a faded reddish " &_
  "sandy brown. The problem here seems to be that to adjust line spacing you need to " &_
  "control positioning  of the view cursor because line spacing is a property of paragraph " &_
  "styles, the creation of which seems to require application of the view cursor, but " &_
  "bizarrely the view cursor  ""knows nothing""  about which paragraph it is currently in; " &_
  "this is apparently ""known"" only to the text cursor !!! So what happens when I try to " &_
  "adjust line spacing is that all paragraphs get adjusted instead of this  particular one. " &_
  "I think that what I may be lacking is a method to create extra paragraph stylenames " &_
  "as the one I'm using seems to behave as a default, so I then could apply specific style " &_
  "names to  specific paragraphs."

  Input3 = "The final text will be written in smaller characters with the Segoe UI Semibold font " &_
  "and coloured in a minty blueish-green. Line spacing here should return to normal" 

  Set oSM = CreateObject ("com.sun.star.ServiceManager")
  Set oDsk = oSM.createInstance(_
  "com.sun.star.frame.Desktop")

  Set oDoc = oDsk.loadComponentFromURL( _
  "private:factory/swriter", "_blank", 0, arg)

  Set oViewCursor = oDoc.CurrentController.getViewCursor()
  Set oParaStyles = oDoc.StyleFamilies.getByName("ParagraphStyles")
  oParaStyleName = oViewCursor.ParaStyleName
  Set oParaStyle = oParaStyles.getByName(oParaStyleName)

  Set objText= oDoc.getText
  Set objCursor= objText.createTextCursor
  objCursor.CharHeight="15"
  objCursor.CharColor = RGB(185,70,45)
  objCursor.CharFontName="Segoe Print"
  objText.insertString objCursor, Input1 , false
  objCursor.gotoEndOfParagraph False 

  objText.insertControlCharacter oViewCursor, 0, False 
  objText.insertControlCharacter oViewCursor, 0, False 

  objCursor.CharHeight="13"
  objCursor.CharPosture=1
  objCursor.CharColor = RGB(10,110,205)
  objCursor.CharFontName="Cambria"
  objText.insertString objCursor, Input2 , false
  oViewCursor.gotoRange objCursor, False

  Set v = oParaStyle.ParaLineSpacing
  v.height = 200
  oParaStyle.ParaLineSpacing = v
  objCursor.gotoEndOfParagraph False 

  objText.insertControlCharacter oViewCursor, 0, False 
  objText.insertControlCharacter oViewCursor, 0, False 

  objCursor.CharHeight="10"
  objCursor.CharPosture=0
  objCursor.CharColor = RGB(180,155,20)
  objCursor.CharFontName="Segoe UI Semibold"
  objText.insertString objCursor, Input3 , false

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 10:20 am
by Zizi64
My first tip is:
Use the Styles (the Paragraph styles in this case) instead of the direct formatting properties.

You can predefine them in a template file, or you can create and apply a new paragraph style by macro too.

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 10:25 am
by Cogito ergo sum
I don't think I'd know how to do that. Can you demonstrate in code how?

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 10:38 am
by Zizi64
My Second tip is:
Download, install, and use one of the object inspection tools like the MRI and the XrayTool. They can list all of the properties, methods, interfaces ... etc of the programming objects.

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 10:43 am
by Zizi64
viewtopic.php?f=45&t=28012
viewtopic.php?f=20&t=77563
viewtopic.php?f=20&t=76871

My third tip is:
Download, and study the Andrew Pitonyak's free macro books.

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 12:16 pm
by JeJe
Instead of using objtext.insertstring use

objCursor.String = Input2

the range of the text cursor will then cover Input2 and you can apply your attributes

objCursor.ParaLineSpacing = v

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 3:26 pm
by Cogito ergo sum
Magic. THank you so much!

Re: Line Spacing in Writer

Posted: Sun May 13, 2018 4:52 pm
by JeJe
Here's an example of creating a custom style with the attrubutes you want and applying it.
As Zizi64 said - its usually the best way.

Code: Select all


newstylename = "NewStyle" 'name for style
vStyle =ThisComponent.createInstance("com.sun.star.style.ParagraphStyle") 'get a style instance

with vstyle 'set properties
Set pspacing = .ParaLineSpacing
  pspacing.height = 200
 .ParaLineSpacing =  pspacing
 .CharFontName="Cambria"
end with

ThisComponent.StyleFamilies.getByName("ParagraphStyles").insertByName(newstylename,vStyle) 'add style to document

'set the style in the document with the viewcursor or a textcursor
oVC = thisComponent.getCurrentController.getViewCursor 
oVC.parastylename = newstylename
for i = 0 to 100
st =st & "In new style "
next
oVC.string =st



Re: Line Spacing in Writer

Posted: Sun May 13, 2018 9:38 pm
by Cogito ergo sum
Problem solved - many thanks!