# How do you put text in a Text Box with a Macro?

How does basic code in a macro add text to a text box? Why isn't there some easily referenced guide for this type of control programming? It would seem that it should be similar to MS Office, ie, TextBox1.Text = "hello", but where is the name of the text box?

edit retag close merge delete

Sort by » oldest newest most voted

Control properties are accessed by a right mouse click in edit mode. The control name is on the General tab. Code to access a TextBox on a Base Form:

oForm = ThisComponent.Drawpage.Forms.getByName("TourFormName") 'Get Form'
oField.Value = (iSaveBalance1 - iSaveBalance2)   'Move in value'


Edit 10/18/16

Code from sample attached:

Sub Main
oForm = ThisComponent.Drawpage.Forms.getByName("Form") 'Get Form'
oField.Text = "Hello"  'Move in value'
End Sub


As you can see, the difference in this code from the originally posted is the Form Name, Text Box Name and the value moved into the textbox.

Here is a working sample using this code in a Writer document: TextBox.odt

more

I got the "Name" of the text box, it's "Text Box 1" and I want to assign a value to be displayed by the text box. The problem is that "Text Box 1" contains spaces, something unusual in a pointer variable name, and when my program attempts to assign a value to the typical designation (TextBox1.Label = "Hello") the interpretor give an assignment error. So obviously the name of the text box is not Text Box 1 or TextBox1. In MS Office, TextBox1.Label = "hello" is valid.

( 2016-10-18 17:17:53 +0200 )edit

First, you have not specified where you are using this text box - Writer, Calc, Base? LibreOffice macros are NOT like MS Office. In the answered example "YourField" is the textbox name and that control is assigned to oField object. Using oField (now pointing to the textbox) you can change things such as .Text or .Value depending upon what you are doing.

( 2016-10-18 18:20:56 +0200 )edit

If you post the code you are trying and which application, I can further clarify.

( 2016-10-18 19:03:07 +0200 )edit

I put a push button and text box in a writer document to interface with the basic language. The push button initiates the code, which opens files and does computations. Those computations are assigned to variables that are to be displayed in the text window. I understand that "yourfield" is the textbox name, let's assume it's the name in the property's field, "Text Box 1". This should be a pointer in itself.

( 2016-10-18 19:09:17 +0200 )edit

sub main oField = oForm.getByName("Text Box 1") oField.Value = "hello" end sub

I get the same error attempting to assign a pointer of Text Box 1 to oField as I did when dereferencing the name without the assignment; TextBox1.Value = "hello"

( 2016-10-18 19:21:44 +0200 )edit

( 2016-10-18 19:47:15 +0200 )edit

( 2016-10-18 19:47:50 +0200 )edit

I'll also point out that the three statements in Sub could be combined into one statement:

ThisComponent.Drawpage.Forms.getByName("Form").getByName("Text Box 1").Text = "Hello"

( 2016-10-18 19:56:32 +0200 )edit

My apologies for this, but I did not point out that your routine was missing the statement to get the form the textbox control was on. This is why you get an error. oForm (used in the oField assignment statement) was never created.

( 2016-10-18 21:40:03 +0200 )edit

I've been trying to insert text into a text box in a Writer Document driven by an event (Changing Page Count). Although I understand and can duplicate Ratslinger's example, in my document, there is no element "Form". In fact by enumeration there are no elements in "Forms". Clearly I am looking for the text box in the wrong place. The text box, crated 'manually' is named "Shape1". What should I use instead of oForm = ThisComponent.Drawpage.Forms.getByName("Form").getByName("Shape1")

( 2017-04-15 12:05:25 +0200 )edit

## Stats

Seen: 3,566 times

Last updated: Oct 18 '16