[Solved] Macro generated word to textbox on main form

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
coopdmc
Posts: 8
Joined: Sat Apr 01, 2017 8:08 am

[Solved] Macro generated word to textbox on main form

Post by coopdmc »

I wrote a Basic macro that generates random words and show it in a msgbox. I'd like to send that random word to a textbox on the main form
Most of the solutions use drawpage to get the MainForm, but causes errors

Code: Select all

(dim oForm as object
dim oControl as object
'oForm = frm.getByName("MainForm")
'oForm = ThisDatabaseDocument.FormDocuments.getByName("MainForm")
'oControl = ofForm.GetByName("Text Box 1")
oForm = ThisDatabaseDocument.FormDocumens.getByName("myForm")
oListBox = oForm.getByName("myListBox")
msgbox(oListBox.CurrentValue)

'the random word code:
'Ascii codes
'48-57 0-9
'65-90 A-Z
'97-122 a-z
'33-38 special characters

Dim Rand As String
Dim hi as integer 
dim low as integer
dim loop2 as integer
for loop2 = 1 to 4
low = 65
hi = 90
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 48
hi = 57
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 97
hi = 122
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 33
hi = 38
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

next
msgbox(rndm1)
Last edited by coopdmc on Thu Apr 06, 2017 1:10 pm, edited 2 times in total.
open office 4.1.3 on windows 10 home pc
F3K Total
Volunteer
Posts: 1038
Joined: Fri Dec 16, 2011 8:20 pm

Re: macro generated word to textbox on main form

Post by F3K Total »

You have to differ between the Formdocument and the structural forms in it

Code: Select all

oFormdocument = ThisDatabaseDocument.FormDocumens.getByName("myForm").open
oForm = oFormdocument.Drawpage.forms.getByName("MainForm")
oListBox = oForm.getByName("myListBox")
msgbox(oListBox.CurrentValue)
R
  • MMove 1.0.6
  • Extension for easy, exact positioning of shapes, pictures, controls, frames ...
  • my current system
  • Windows 10 AOO, LOLinux Mint AOO, LO
coopdmc
Posts: 8
Joined: Sat Apr 01, 2017 8:08 am

Re: macro generated word to textbox on main form

Post by coopdmc »

Thanks R3K Total, these two lines cause errors:
oFormdocument = ThisDatabaseDocument.FormDocuments.getByName("MainForm").open
oForm = oFormdocument.Drawpage.forms.getByName("MainForm")

Error:
BASIC Runtime Error.
object variable not set

I think the trouble is My system doesn't recognize "Drawpage" or "Drawform" (?), as I tried to explain in my original inquiry (although not very well explained).

The original example of displaying a List Box text, I believe used UNO Services. Is anyone familiar with UNO?
open office 4.1.3 on windows 10 home pc
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: macro generated word to textbox on main form

Post by UnklDonald418 »

Substitute "myForm" for "MainForm" and I believe you will eliminate the error(s) you are seeing.

Is "myListBox" really a List Box or just a poorly chosen name for a Text Box?
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: macro generated word to textbox on main form

Post by UnklDonald418 »

If it is a Text Box use

Code: Select all

oListBox.setString(rndm1)
to display the results in the Text Box.
If it is actually a List Box the code gets a bit more complicated.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
coopdmc
Posts: 8
Joined: Sat Apr 01, 2017 8:08 am

Re: macro generated word to textbox on main form

Post by coopdmc »

Thanks, all for responding, sorry F3K Total, (not R3k Total). I still don't have a complete answer (how does one set a form text box value from a macro result?) however I have found a small part of the puzzle (how to get a form text box value and display in msgbox?)
I found this bit of code at Open Office Forums
viewtopic.php?f=20&t=84203

'Sub Macro1
'Dim x as string
'Dim oDoc as object
'oDoc = ThisComponent
'x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("Form").getByName("Text Box 1").string
'msgbox x
'End Sub
'Sub Macro2(oEvent as object)
'Dim x as string
'x= oEvent.source.Text
'msgbox x
'end sub

which I have modified to work as follows:

Dim x as string
Dim oDoc as object
Dim oForm as object
oDoc = ThisComponent
x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("MainForm").getByName("Text Box 1").string
msgbox x

This shows the Text Box value from a form to a msgbox.
New question:
How do I use a variable (token?) from the macro to populate a unbound Text Box on the form?
Thoughts, how to extract the Text Box (Dim otextbox as object, otextbox.value = ?), in order to address the Text Box directly?
open office 4.1.3 on windows 10 home pc
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: macro generated word to textbox on main form

Post by UnklDonald418 »

Maybe this will help. I uploaded a small demonstration with a simple form (myForm) containing a text box and a button. The button runs a macro that generates a random string (from your code) and displays the result in the unbound text box.
Attachments
Demo08.odb
(12.33 KiB) Downloaded 207 times
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
coopdmc
Posts: 8
Joined: Sat Apr 01, 2017 8:08 am

Re: macro generated word to textbox on main form

Post by coopdmc »

Looks promising, I just get in the text box "G4e"H2m"E2g#E1h", subsequent button pushes don't affect the text box. Maybe it needs a form.refresh() in the code?
I am unable to modify your code, it's locked (certainly understandable).
open office 4.1.3 on windows 10 home pc
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: macro generated word to textbox on main form

Post by UnklDonald418 »

I am unable to modify your code, it's locked
Must be something on your end. I just downloaded it to another computer and it generates a different value each time I click on the button, and it is not locked.
Create a new database and drag the form onto the new database. Then copy the macro code. You may have to re-link the button event to the location where you saved the macro code.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
coopdmc
Posts: 8
Joined: Sat Apr 01, 2017 8:08 am

Re: macro generated word to textbox on main form

Post by coopdmc »

I was able to get working the load a forms unbound textbox with macro data (UnklDonald418, Demo08.odb)
viewtopic.php?f=45&t=20279

Code: Select all

REM  *****  BASIC  *****
'set textbox to macro data (macro to text)
Sub Main
'Function Randm(Num1 As Integer, Num2 As Integer)
'48-57 0-9
'65-90 A-Z
'97-122 a-z
'33-38 spec
Dim Rand As String
Dim hi as integer 
dim low as integer
dim loop2 as integer
for loop2 = 1 to 4
low = 65
hi = 90
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 48
hi = 57
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 97
hi = 122
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

low = 35
hi = 38
Randomize
rndm1 = rndm1 & Chr$(Int((hi - low + 1) * Rnd + Low))

next
'msgbox rndm1
oDoc = ThisComponent
oDocView = oDoc.getCurrentController()

oForm = oDoc.drawpage.forms(0)
oTextBox = oForm.getByName("Text Box 1")

oDocView.getControl(oTextBox).setFocus()
oTextBox.text = rndm1
'end of macro data to textbox

'Get textbox value:  This code gets the forms text box and displays the text in a msgbix
Dim x as string
Dim oDoc as object
'Dim oForm as object
oDoc = ThisComponent
x = oDoc.CurrentController.Model.DrawPage.Forms.getByName("MainForm").getByName("Text Box 1").text
msgbox x
'end Get Textbox value.
For us "new to OOBasic":

If you copy code from a volunteer, that code is particular to that persons form. Build your own form and be sure that the form is running (not 'design' mode).
open office 4.1.3 on windows 10 home pc
Post Reply