Insert and Read Text in TextField Control Using LibreOffice Basic Macro


This tutorial will show how to default a string, insert a string at runtime, reading the string from a TextField control using Basic macro.

Pre-requisite

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.

Using form controls
Using dialog controls

If you are looking for something else, head over to below link which contains all the LibreOffice macro tutorials covered:

Macro Tutorial Index

TextField – How to Add in a Dialog

To add a TextField, click below icon and drag it to a new dialog.

How to read text from TextField

To read the text from TextField, use getText() method. It will return the input text as string from TextField.
Example:

oTextField = oDialog1.getControl("TextField1")
Msgbox oTextField.getText()

How to insert or update the TextField text

During runtime, to update the contents of TextField, use setText() method.
Example:

oTextField = oDialog1.getControl("TextField1")
oTextField.setText("Hello World!!!")

How to default a text in a TextField

There are two ways you can default a text in a TextField. During design you can set any default Text for a TextField using Text property. Type anything you want in Text property and it would be filled in TextBox.

Second option is to use setText() method while loading the dialog with its controls.

TextField default Text property

Function References – Used in this article

XTextComponent

Complete Macro

To run, Copy and paste this entire code block below in Macro Editor in LibreOffice. Create a dialog with a button and a text box and then assign the test() method to the button’s click event.
Read Text

Dim oDialog1 As Object
 
Sub StartDialog1()
    BasicLibraries.LoadLibrary("Tools")
    oDialog1 = LoadDialog("Standard", "Dialog1")
    oDialog1.Execute()
End Sub

Sub test()
	oTextField = oDialog1.getControl("TextField1")
	Msgbox oTextField.getText()
End Sub
 

Set Text

Dim oDialog1 As Object
 
Sub StartDialog1()
    BasicLibraries.LoadLibrary("Tools")
    oDialog1 = LoadDialog("Standard", "Dialog1")
	oDialog1.Execute()
End Sub

Sub test()
	oTextField = oDialog1.getControl("TextField1")
	oTextField.setText("Hello World!!!")
End Sub
 

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:

LibreOffice Macro Tutorial Index

Exit mobile version