Read Selected Text and Select Part of Text in TextField Control Using LibreOffice Basic Macro
This tutorial will show how to select a part of text and how to read the selected text from a TextBox control in LibreOffice using Basic macro.
This tutorial assumes that you know how to create a simple macro and a simple dialog. If you are unaware of the same, read below two tutorials before you proceed.
If you are looking for something else, head over to below link which contains all the LibreOffice macro tutorials list:
TextField – How to Add in a Dialog
How to read Selected Text
To read the selected portion of the text box string, use
getSelectedText() method of textbox control.
oTextField = oDialog1.getControl("TextField1") MsgBox oTextField.getSelectedText()
How to select a part of textbox text
To select a substring of textbox text, use
setSelection() method takes the
Struct Selection as argument. Structure
Selection have two elements i.e.
Min speifies the lower limit of the range and
Max refers to upper limit of the range.
Before using the
Selection structure, it needs to be defined and loaded with values. To define a structure use
createUnoStruct function which takes
com.sun.star.awt.Selection as argument to create instance of selection structure.
After creating the selection instance, the Min and Max values can be set.
Dim oSelection oSelection = createUnoStruct( "com.sun.star.awt.Selection" ) With oSelection .Min = 5 .Max = 7 End With
Then the selection object can be passed into
setSelection method as argument.
oTextField = oDialog1.getControl("TextField1") oTextField.setSelection(oSelection) oTextField.setFocus()
To run, Copy and paste this entire code block below in Macro Editor in LibreOffice.
Dim oDialog1 As Object Sub StartDialog1() BasicLibraries.LoadLibrary("Tools") oDialog1 = LoadDialog("Standard", "Dialog1") oDialog1.Execute() End Sub Sub test() Dim oSelection 'Libreoffice oSelection = createUnoStruct( "com.sun.star.awt.Selection" ) With oSelection .Min = 4 .Max = 7 End With oTextField = oDialog1.getControl("TextField1") oTextField.setSelection(oSelection) oTextField.setFocus() End Sub
Function References – Used in this article
Looking for Something Else?
If you are looking for something else in LibreOffice macro tutorials, Or, wants to learn more about it, please follow below link for complete Macro Tutorials Index: