Ask Your Question
1

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

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

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 close merge delete

1 Answer

Sort by » oldest newest most voted
0

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

Ratslinger gravatar image

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

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

Comments

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 +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.

Ratslinger gravatar imageRatslinger ( 2016-10-18 18:20:56 +0200 )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 +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.

jerryemerydelacruz gravatar imagejerryemerydelacruz ( 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"

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

Please se edit in original answer.

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

Please see edit in original answer.

Ratslinger gravatar imageRatslinger ( 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"
Ratslinger gravatar imageRatslinger ( 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.

Ratslinger gravatar imageRatslinger ( 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")

Pansmanser gravatar imagePansmanser ( 2017-04-15 12:05:25 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

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

Seen: 3,566 times

Last updated: Oct 18 '16