[Solved] Start macro from button in dialog and close dialog

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
frapelli
Posts: 3
Joined: Sun Jan 05, 2020 1:47 pm

[Solved] Start macro from button in dialog and close dialog

Post by frapelli »

I have created a dialog with some buttons.

I assigned a macro to each button (all macros send to a different sheet of the document, kind of "Go to Sheet 1, Sheet2, and so on).

The macros runs, but i need that clicking on a button, besides activating the macro (sending the the selected sheet), should also close the dialog containing the buttons,which does not happen.

Thanks.
Last edited by robleyd on Tue Jan 07, 2020 1:08 am, edited 2 times in total.
Reason: Add green tick
openoffice 4.16 linux/windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Start macro from button in a dialog and close the dialog

Post by JeJe »

You can put YourDialogName.endexecute after your button code or you can change the Button type from default to OK
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
frapelli
Posts: 3
Joined: Sun Jan 05, 2020 1:47 pm

Re: Start macro from button in a dialog and close the dialog

Post by frapelli »

Thanks for your answer.

OK type on button close dialog but does not execute macros

What you mean by "put YourDialogName.endexecute after your button code"? Button has no code in dialog, and adding YourDialogName.endexecute (which in my case is dlgScegli.endexecute) in the code of macros started by buttons gives error message (variable not defined).
openoffice 4.16 linux/windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Start macro from button in a dialog and close the dialog

Post by JeJe »

If you use the okay button you need to put your code somewhere like the mouse pressed event.

For a normal button (the better option) you use a function to return your dialog name (loaddialog) which you store at the top of the module, you can then use it to call .endexecute.

See attached writer document with this implemented.

Code: Select all


REM  *****  BASIC  *****
dim dialogname

sub loaddialog1
dialogname = loaddialog("Standard","Dialog1")
dialogname.execute

end sub

Sub OkayButtonMousePressed
dialogname.model.title = "okay button"
End Sub

Sub OrdinaryButtonExecute
msgbox "ordinary button"
dialogname.endexecute
End Sub


Function LoadDialog(oLibraryName as String, oDialogName as String)
	Dim oLib as Object
	DialogLibraries.LoadLibrary(oLibraryName)
	oLib = DialogLibraries.GetByName(oLibraryname)
	LoadDialog() =  CreateUnoDialog(oLib.GetByName(oDialogName))
End Function

Edit: corrected can't use msgbox with okay button changed above and in document
Attachments
dialog close.odt
(10.85 KiB) Downloaded 229 times
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Start macro from button in a dialog and close the dialog

Post by Villeroy »

Assign the button type property to an OK button and a Cancel button. Both buttons will close the dialog and let the execute method return 1 or 0.

Code: Select all

REM  *****  BASIC  *****
dim dialogname

sub loaddialog1
dialogname = loaddialog("Standard","Dialog1")
x = dialogname.execute
if x = 1 then
  do_this
else
  do_that
endif
REM the dialog will be closed now
end sub
The execute method returns 1 if a button of type "OK button" has been pressed.
The execute method returns 0 if a button of type "Cancel button" has been pressed or the dialog has been closed by the [x] button in the title bar.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Start macro from button in a dialog and close the dialog

Post by JeJe »

The other way to get the dialog is from the event

Code: Select all

sub button_execute(ev) 'call with button execute or other event
'your code
ev.source.getcontext.endexecute
end sub

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
frapelli
Posts: 3
Joined: Sun Jan 05, 2020 1:47 pm

Re: Start macro from button in a dialog and close the dialog

Post by frapelli »

All solutions worked fine. Thanks to all of you.
openoffice 4.16 linux/windows 10
Post Reply