2

I want to change the font and set its style to bold. I have two problems:

  • changing CharWeight works but not CharFontName
  • it applies "bold" to the whole paragraph, not only to the selection

Here's my code:

sub AddAnimation
    xTextCursor = ThisComponent.CurrentController.Selection(0)
    xText = xTextCursor.getText()
    xText.CharFontName = "Consolas"
    xText.CharWeight = com.sun.star.awt.FontWeight.BOLD
end Sub
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • What is the font name currently set to? Try starting with an empty document and creating one text block. – Jim K Apr 03 '17 at 17:51

1 Answers1

1

Calling getText() gets the entire text, not just the selected part.

Sub ChangeFont
    xTextCursor = ThisComponent.CurrentController.Selection(0)
    xTextCursor.CharFontName = "Consolas"
    xTextCursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
End Sub

The font name changed when I tried it, using both LO and AOO.

result

Are you using CTL or CJK scripts? If so, then it needs to be CharFontNameComplex or CharFontNameAsian. However if CharWeight worked, then that must not be the issue.

One more guess: Maybe a style is overriding it.

Jim K
  • 12,824
  • 2
  • 22
  • 51