Text fields in dialog boxes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ABHicks
Posts: 3
Joined: Thu Aug 02, 2018 8:58 am

Text fields in dialog boxes

Post by ABHicks »

I am experimenting with OpenOffice Basic programmimg.

I have a spreadsheet document "Experiment1" which contains a worksheet called "Test Sheet", which contains a Dialog box called "Dialog1" (which resides in a "Standard" Library inside ""My Dialogs"), which includes a Text Field called "Textfield1".

I want to be able to enter a name into the text field and then put it into a specified cell, say B25.
I can activate the dialog box and I have a macro in Module 1 in the library called "TextField1Activated" which contains the lines given below.
I have set the macro to be run on the Event "When losing focus", which allows me to enter text into the text field box.
However, nothing happens when I press enter, and when I move the focus away from this text field I get an "Object variable not set" error message at the green line in the listing below. Obviously this line is wrong, but I cannot find any guidance or examples anywhere to show me what is should read.

Code: Select all

' get cell B25
 dim sheet as object
 dim cell as object
 dim doc as object
doc=thiscomponent
sheet=doc.sheets.getbyname("Test Sheet")
cell=sheet.getcellbyposition(1,24) ' this is cell B25 I think

dim textfieldOnedialog as object
dim textfieldOne as object

textfieldOnedialog=CreateUnoDialog(DialogLibraries.Standard.getByName("Dialog1"))
textfieldOne=textfieldOnedialog.getcontrol("Textfield1")
[color=#008040]
cell.value = textfieldOne.text[/color] ' this is a guess on my part
What do I do now to actually place the text that I have just put in the text field box into cell B25?

Any help would be most welcome.

Thank you in advance
Andrew
Last edited by robleyd on Wed Aug 08, 2018 10:23 am, edited 1 time in total.
Reason: Added Code tags
Open Office 4.1.5 Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Text fields in dialog boxes

Post by FJCC »

I would do it like this.

Code: Select all

Dim oDialog1 as Object

Sub Main

DialogLibraries.LoadLibrary( "Standard" )
oDialog1 = CreateUnoDialog( DialogLibraries.Standard.Practice )
oDialog1.Execute()

End Sub

Sub CopyText

oModel = oDialog1.getModel()
oTextfield = oModel.getByName("TextField1")

oSheet = ThisComponent.Sheets.getByName("Sheet1")
oCell = oSheet.getCellrangeByName("B27")

oCell.String = oTextField.Text
End sub

Sub EndDialog
 
oDialog1.endExecute()
  
End Sub
Notice that oDialog1 is defined as a global variable, outside of any Sub.
Sub Main is called by a button on the spreadsheet, starting the dialog.
Sub CopyText is called when TextField1 loses focus
Sub EndDialog is called by a button on the dialog labeled Close.
The text of TextField1 is assigned to the String property of oCell, not to the Value.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
ABHicks
Posts: 3
Joined: Thu Aug 02, 2018 8:58 am

Re: Text fields in dialog boxes

Post by ABHicks »

Thank you for your reply. I apologise for the delay in responding but my Internet connection went down and I have only just got it back!

I will try your suggestion

Many thanks

Andrew
Open Office 4.1.5 Windows 10
Post Reply