Ask Your Question

How do you put text in a Text Box with a Macro? [closed]

asked 2016-10-18 04:00:44 +0100

jerryemerydelacruz gravatar image

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 flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by Alex Kemp
close date 2020-09-13 22:16:03.350972

1 Answer

Sort by » oldest newest most voted

answered 2016-10-18 04:26:34 +0100

Ratslinger gravatar image

updated 2016-10-18 19:46:50 +0100

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 = oForm.getByName("YourField")   'Get access to field'
oField.Value = (iSaveBalance1 - iSaveBalance2)   'Move in value'

There is documentation at this link (click here) and the most comprehensive single document OOME (click here for PDF version).

Edit 10/18/16

Code from sample attached:

Sub Main
oForm = ThisComponent.Drawpage.Forms.getByName("Form") 'Get Form'
oField = oForm.getByName("Text Box 1")   'Get access to field'
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

edit flag offensive delete link 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.

jerryemerydelacruz gravatar imagejerryemerydelacruz ( 2016-10-18 17:17:53 +0100 )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.

Ratslinger gravatar imageRatslinger ( 2016-10-18 18:20:56 +0100 )edit

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

Ratslinger gravatar imageRatslinger ( 2016-10-18 19:03:07 +0100 )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.

jerryemerydelacruz gravatar imagejerryemerydelacruz ( 2016-10-18 19:09:17 +0100 )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"

jerryemerydelacruz gravatar imagejerryemerydelacruz ( 2016-10-18 19:21:44 +0100 )edit

Please se edit in original answer.

Ratslinger gravatar imageRatslinger ( 2016-10-18 19:47:15 +0100 )edit

Please see edit in original answer.

Ratslinger gravatar imageRatslinger ( 2016-10-18 19:47:50 +0100 )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"
Ratslinger gravatar imageRatslinger ( 2016-10-18 19:56:32 +0100 )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.

Ratslinger gravatar imageRatslinger ( 2016-10-18 21:40:03 +0100 )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")

Pansmanser gravatar imagePansmanser ( 2017-04-15 12:05:25 +0100 )edit

Question Tools

1 follower


Asked: 2016-10-18 04:00:44 +0100

Seen: 5,273 times

Last updated: Oct 18 '16