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?
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
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.
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.
If you post the code you are trying and which application, I can further clarify.
If you post the code you are trying and which application, I can further clarify.
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.
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”
Please se edit in original answer.
Please see edit in original answer.
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"
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.
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”)
If you enter form design mode and add a text box, it will automatically create a form named “Form” which can be changed by the user. It seems your text box was not created in this fashion. Please ask this in a new question and include how you created this text box. Also I don’t know of a standard event named “Changing Page Count”. Please include what you are trying to accomplish with that. LO version and OS info may help.