Search code examples
libreofficelibreoffice-basic

Bold text in LibreOffice using macros (LibreOffice Basic)


I am doctor (X-ray). Asking my friend to create text macros for me to write protocols faster. I just wanted to push ALT+Q (or another letter). Then push ALT+number (from 0 to 9). For each ALT+number I insert prepared text. It is work. Part of inserted text should be bold. I insert text, then select 2-3 words and press Ctrl+B. But I wanted to customised macro.

I need: vvod(0).Value = //bold "Вариант строения bold// — // regular один корень, один канал. regular//"

I tried to figure it out on my own, but I can't, because I'm a piece of shit. Can someone tell me in which direction to think, because I'm stuck?

I am attaching part of the code.

enter code here

    REM ***** BASIC ***** 

GLOBAL number as Integer 
GLOBAL vid as Integer 
public Sub Main 
End Sub 

public Sub ALTQ 
vid = 1 
End Sub 

public Sub ALTW 
vid = 2 
End Sub 

public Sub ALTE
vid = 3 
End Sub 

public sub ALT1 
number = 1 
solve() 
end sub 

public sub ALT2 
number = 2 
solve() 
end sub 

public sub ALT3 
number = 3 
solve() 
end sub 

public sub solve 
rem —-----------------------------------------------------------------------------------— 
rem Определение переменных и получение доступа к ним, не трогай 
dim document as object 
dim dispatcher as object 
document = ThisComponent.CurrentController.Frame 
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") 
rem —---------------------------------------------------------— 
rem определение переменных, которые ты сам будешь создавать и использовать 
dim vvod(0) as new com.sun.star.beans.PropertyValue 
rem Name обязательно равняется ТЕКСТ 
vvod(0).Name = "Text" 
rem —------------------------------------------------------------------------------------— 

if vid = 1 Then 
if number = 1 Then 
vvod(0).Value = "коронковая часть реставрирована. " 
elseif number = 2 Then 
vvod(0).Value = "коронковая часть реставрирована пломбами. " 
elseif number = 3 Then 
vvod(0).Value = "депульпирован. Коронковая часть реставрирована. " 
End if 

I tried to figure it out myself and this is what I came up with:

1)

Dim sVar As String
sVar = "Это тест"
sVar.CharWeight = com.sun.star.awt.FontWeight.BOLD

error

2)

Dim TextX As Object
TextX.String = "Это тест"
RectangleShape.CharWeight = com.sun.star.awt.FontWeight.BOLD
vvod(0).Value = TextX

error


I will be very grateful if you help me.


Solution

  • I'm unable to give exact details because I don't use Libreoffice Basic but in python you need to grab the cursor and apply the font attributes to that.
    i.e. something along the lines of:

    desktop = XSCRIPTCONTEXT.getDesktop()
    cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
    oFontWeight = cursor.CharWeight
    Text_Weight = FontWeight.BOLD
    cursor.CharWeight=Text_Weight
    text.insertString(cursor, insert_text, 0)
    cursor.CharWeight=oFontWeight
    

    In principle, grab the current cursor and make a note of the current setting.
    Apply Bold text to the cursor
    Insert text using the adjusted cursor
    Reset cursor to previous text setting, or everything after this will be Bold.

    p.s. I'm still grinning at your description of yourself. Good luck!

    The above assumes:

    model = desktop.getCurrentComponent()
    text = model.Text
    

    and that you imported com.sun.star.awt.FontWeight