[Solved] Line Spacing in Writer

Creating a macro - Writing a Script - Using the API

[Solved] Line Spacing in Writer

Postby Cogito ergo sum » Sun May 13, 2018 8:47 am

Can someone please help with this?
Code: Select all   Expand viewCollapse view
' =========================
' 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
Last edited by robleyd on Mon May 14, 2018 8:19 am, edited 1 time in total.
Reason: Tagged [Solved] [robleyd, Moderator]
OpenOffice 5.3.6.1 on Windows 7
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Postby Zizi64 » Sun May 13, 2018 10:20 am

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.
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.0 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7201
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Postby Cogito ergo sum » Sun May 13, 2018 10:25 am

I don't think I'd know how to do that. Can you demonstrate in code how?
OpenOffice 5.3.6.1 on Windows 7
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Postby Zizi64 » Sun May 13, 2018 10:38 am

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.
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.0 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7201
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Postby Zizi64 » Sun May 13, 2018 10:43 am

Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.0 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 7201
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Line Spacing in Writer

Postby JeJe » Sun May 13, 2018 12:16 pm

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
Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 239
Joined: Wed Mar 09, 2016 2:40 pm

Re: Line Spacing in Writer

Postby Cogito ergo sum » Sun May 13, 2018 3:26 pm

Magic. THank you so much!
OpenOffice 5.3.6.1 on Windows 7
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am

Re: Line Spacing in Writer

Postby JeJe » Sun May 13, 2018 4:52 pm

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   Expand viewCollapse view

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


Openoffice 4.1.2
Windows 8
JeJe
 
Posts: 239
Joined: Wed Mar 09, 2016 2:40 pm

Re: Line Spacing in Writer

Postby Cogito ergo sum » Sun May 13, 2018 9:38 pm

Problem solved - many thanks!
OpenOffice 5.3.6.1 on Windows 7
Cogito ergo sum
 
Posts: 13
Joined: Sun May 13, 2018 8:40 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests