[Closed] Click a button in another file to run its macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

[Closed] Click a button in another file to run its macro

Post by OldStd »

13 November 2018

I have found an [close enough]/[acceptable] answer from another learner from his/her post, and hence marked the topic as [Closed],
because it is not clicking a button in the other file to run the macro assigned to it.
Re: Run Macro in Another File
Postby Math » Tue Nov 13, 2018 12:48 pm
Many thanks to the friends who helped me,
I was able to find a solution on the following link:
https://ask.libreoffice.org/pt-br/quest ... -planilha/
hugs to all.

Original title
How to click a button in order to run a macro assigned to another button


In following another topic in this forum I have come across this topic, and the one who posted the question marked it as [Solved]

I copied and adapted the [solution – codes] and tried out the codes.

While I could clearly see that second button was being [pressed] by the macro assigned to the first button, the macro assigned to the second button, the one that had been [pressed] by the codes didn't run.

What went wrong?

Code: Select all


REM  *****  BASIC  *****
option explicit

sub testa
	Dim oDoc As Object
	Dim oSheet As Object
	Dim oDP As Object
	Dim oButton As Object
	dim oPage as object
	oDoc = ThisComponent
	oSheet = oDoc.getSheets().getByIndex(0)
	oDP = oSheet.getDrawPage()
	oPage = oDp.getbyIndex(1)
	oButton = oPage.getControl(1)

	msgbox "oButton.name= " & oButton.name  ''' oButton.name = btntestpushbtn (that is correct)

	oButton.State = 1   ''' btntestpushbtn was seen pressed, but the assigned codes didn't run
	Wait(500)
	oButton.State = 0
	oButton.reset()
end sub

sub testpushbtn
    msgbox "This is filea.ods a LO_Calc workbook"
    rem If this button was pressed on its own, the above msgbox would appear as expected.
end sub

Last edited by OldStd on Tue Nov 13, 2018 5:15 pm, edited 1 time in total.
Openoffice 4.15
Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to programmatically click a button to run assigned m

Post by Villeroy »

May I ask which books on programming you are reading?
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
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

OO ebooks
OO ebooks
JS ebooks
JS ebooks
js_ebooks.png (6.69 KiB) Viewed 3828 times
eBooks I am currently reading or browsing
eBooks I am currently reading or browsing
py_ebooks.png (4.48 KiB) Viewed 3828 times
Hi. Villeroy
The codes above were adapted from the reply to this topic.
Re: [Solved] Macro that pushes a button
Postby Charlie Young » Mon Apr 14, 2014 6:56 pm

Code: Select all

Sub Pushthatbutton()
   Dim oDoc As Object
   Dim oSheet As Object
   Dim oDP As Object
   Dim oButton As Object
   
   oDoc = ThisComponent
   oSheet = oDoc.getSheets().getByIndex(0)
   oDP = oSheet.getDrawPage()
   oButton = oDP.getByIndex(0)
   oButton.getControl().State = 1
   Wait(100)
   oButton.getControl().State = 0
   oButton.getControl().reset()
End Sub
I have made a screenshot for the titles of ebooks that I am reading on OO Basic, JS, and Python for your reference.

Please note that some titles are not books but some webpages I [printed] that contains some relevant topics.

I hope you can give me some advice as to what are the more appropriate books to read.
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to programmatically click a button to run assigned m

Post by Zizi64 »

How to programmatically click a button to run assigned macro
Why you want to CLICK on the button programatically? Just call the assigned macro routine directly.

If you know the name of the routine to launch: it is a very simple task.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Hi. Zizi64.

I was following another thread where the author asked how to run the macros in another document.

One answer was to place all the macros in [myMacros] where the macros can be assessed by every document. But the author didn't like the solution because there would too many macros centralized in one place. He (or she) wants the macros to remain in the files where the macros were originally written.

My own answer was to create a master file, copy the macros from each file to a module marked for macros for that file, and modify the macro to take the document as an input parameter, and run the macros from the master file.

After that I imagine, what if I create a pushbutton in each of the individual file and get the master file to assess the buttons in order to run the macros residing in the individual files? The answer is that the button can be pressed but the macro won't run.

I have no idea why the macro won't run, in this case.

Why did the codes work for other people?
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Villeroy wrote:May I ask which books on programming you are reading?
Hi. Villeroy.
I am wondering why you asked this question.
Have I said something about Python or other language features that are totally out of point? If so, it is because while I had a quick glance at some of the books, I had not tried out the codes, or tried to fully understand the features. That would require second and many re-reading of the topics.
Right now, I am trying to find an IDE for Python. I download the [Atom], but I am intimidated with the long documentation on how how to use it.
Is there some easier to use IDE for a beginner?
I had undergone an introductory course in Python a couple of years ago. But due to lack of use and practice, I have forgotten how to go about using Python by now and have to start from scratch all over again.
Today, I have ploughed through [Python Basics, by S.R. Doty; August 27, 2008]. This is a 20-page brief introductory course.
And also [Introduction to Python; Heavily based on presentations by Matt Huenerfauth (Penn State); Guido van Rossum (Google); Richard P. Muller (Caltech)].
A 87-page slide-presentation style book introducing briefly main Python features.
I am ready to try my hands on Python if I can find an easy to use IDE.
Openoffice 4.15
Windows 10
User avatar
robleyd
Moderator
Posts: 5083
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: How to programmatically click a button to run assigned m

Post by robleyd »

Perhaps this search will help you find an IDE; the easy to use part is subjective and will vary from person to person.

You'll have to try an assortment of IDEs until you find one that suits you, if there be such a beast.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to programmatically click a button to run assigned m

Post by Lupp »

Zizi64 wrote:Why you want to CLICK on the button programatically? Just call the assigned macro routine directly.
If a Sub is called due to mouse event on a PushButton, e.g, it gets passed a MouseEvent object as parameter. The parameter also gives access to the control via its property .Source.Model .

In the few cases I wrote code for such Sub I mostly wanted to evaluate the button's .Tag property as a kind of surrogate for parameters - or as a specic way to pass parameters if you prefer to put it this way.

(Anyway, I would be interested to know if there actually is a way to make a control raise an event that not actually occurred as if it was induced by user interaction. Or - in other words again - to raise an event simulating a control-event.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2780
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to programmatically click a button to run assigned m

Post by JeJe »

(Anyway, I would be interested to know if there actually is a way to make a control raise an event that not actually occurred as if it was induced by user interaction. Or - in other words again - to raise an event simulating a control-event.)
In Windows, it may be possible using Windows Api calls see here:

http://www.vbforums.com/showthread.php? ... g-Keyboard

If the mouse_event function can be called from OOBasic then its just a question of working out the position of the button and making sure its window is on top of any others.

Alternatively there are 3rd party programs like Autoit.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to programmatically click a button to run assigned m

Post by Villeroy »

Lupp wrote: (Anyway, I would be interested to know if there actually is a way to make a control raise an event that not actually occurred as if it was induced by user interaction. Or - in other words again - to raise an event simulating a control-event.)
Create the right type of event struct, fill it with adequate data and call the event routine with that struct.
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
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to programmatically click a button to run assigned m

Post by Lupp »

Villeroy wrote:Create the right type of event struct, fill it with adequate data and call the event routine with that struct.
Yes, I was afraid this would be your answer.
Regards.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to programmatically click a button to run assigned m

Post by Villeroy »

easy
Attachments
roll_your_action_event.odt
(12.22 KiB) Downloaded 156 times
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
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Villeroy wrote:easy
Hi. Villerory.

Thank you for your attached codes.
I tested it out. It worked well in a single file where the 2 macros are both residing in the same file.
When I separated the [main] macro to another file and leaving the macro [click_button] in its original file, the code failed to work.

Error message [sub-procedure or function-procedure not defined].

What our friend in his original question is paraphrased to be like this.

We are having a centralized command system where the commander gives the instruction, [the calling macro], and each of the divisional officers will carry out the order, [executing macros] that stay in the individual files.

Of course, if we transfer the macros from the individual files to the modules in the central command file, the problem doesn't arise at all.

My interpretation is attached in a 7zip file
Attachments
centralcom.7z
A folder with 2 files
(16.28 KiB) Downloaded 152 times
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

robleyd wrote:Perhaps this search will help you find an IDE; the easy to use part is subjective and will vary from person to person.

You'll have to try an assortment of IDEs until you find one that suits you, if there be such a beast.
Thanks for the link.

I am going to try Thonny first.
Easy to get started. Thonny comes with Python 3.7 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Greetings. Jeje and Lupp.

Thanks for the info.
As these are a bit advance, it'll take me sometime to learn up and appreciate what you people are saying.

Similarly, I don't quite understand the codes from Villerory either.
Openoffice 4.15
Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to programmatically click a button to run assigned m

Post by Villeroy »

OldStd wrote:Greetings. Jeje and Lupp.

Thanks for the info.
As these are a bit advance, it'll take me sometime to learn up and appreciate what you people are saying.

Similarly, I don't quite understand the codes from Villerory either.
The above document was aimed at Lupp. You don't even understand procedural programming, let alone parameters.
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
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to programmatically click a button to run assigned m

Post by Lupp »

Villeroy wrote:easy
Hmmm. Very easy. Everybody expected to get the Model from the Form object, and to need to ask the CurrentController for the Control needed to pass everything as a Source to an event. Everybody but me, I should add, probably.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to programmatically click a button to run assigned m

Post by Villeroy »

Well, if you try to program Base forms you know more unreasonable demands than this one. I hate it too.
It's easier with dialogs where you have the loaded and clickable controls on the loaded dialog and their models.

Clickable database form controls attached to a document belong to the controller. The models belong to a draw page's forms container. In this example you dive into the known hierarchy of form and control models and then query the clickable control from the controller by passing the model. Contrary to dialogs, database forms and controls may build up complex and deep hierarchies reflecting a database hierarchy.

However, all this is not what the original poster asked for.
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
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Greetings Villerory and Lupp.

Thanks for taking your time to answer and comment.

With the remarks from Villerory I realised I lack some elementary pieces of knowledge.

With Lupp:s comments I understand that I couldn't understand the codes because I have not yet learned about dialog, forms and database form in AOO.

Now I feel better because I know what are the next steps to take in my learning.

Since it is a lot of work and I am itching to get the Sudoku puzzles solved using a new method I am putting the AOO aside and learn some Python first with Thonny as an IDE.

I am still going to use procedural programming with an OOP language.

The OOP part will come later.

As a side remark after I transferred the macro (main) from Villerory to a different document and run it and from the error message (sub procedure or function procedure not defined) the object that is accessed by the macro is a sub procedure and not the clickable button that was desired.

Pardon me if I misinterpreted the error message.

It looks like there is no way to run the macros in document B from Document A,  I propose that we close this discussion unless someone else can come up with a real solution.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: How to programmatically click a button to run assigned m

Post by OldStd »

Greetings: Villerory, Lupp, Zizi64, robleyd, Jeje,
and all others who have lent assistance in the discussion.

I have found an [close enough]/[acceptable] answer from another learner from his/her post, and hence marked the topic as [Closed],
because it is not clicking a button in the other file to run the macro assigned to it.
Re: Run Macro in Another File
Postby Math » Tue Nov 13, 2018 12:48 pm
Many thanks to the friends who helped me,
I was able to find a solution on the following link:
https://ask.libreoffice.org/pt-br/quest ... -planilha/
hugs to all.
Well, the answer is not exactly clicking a button in another file, but directly invoking a macro residing in another file, as suggested by Zizi64 earlier.
In trying out an earlier code sample copied from another post, while the button in the other file can be pressed, the macro assigned to it, was not fired.

I have copied the translation of explanation and the codes here for easy reference for those new learners who are following the discussion, here.

I have tested the codes in my PC with LibreOffice, and the codes worked with no problems.
Translation of explanation:
I happened to find what you are looking for. The following procedure executes a macro named "MyMacro" in the "Standard" library in module "Module1" of another file. It is opened in a hidden way and after running the macro is placed visible.
[/quote]

Code: Select all

Sub ManipulateHiddenDoc
   Dim oDoc
   Dim oVal(1) As New com.sun.star.beans.PropertyValue
   Dim oscriptProvider
   Dim oScript

   oVal(0).Name = "Hidden"
   oVal(0).Value = True
   oVal(1).Name = "MacroExecutionMode"
   oVal(1).Value = com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN

   oDoc = StarDesktop.loadComponentFromURL("file:///c:/tmp/a.odt", " blank", 0, oVal())
   oscriptProvider = oDoc.getScriptProvider()
   oScript = oscriptProvider.getScript("vnd.sun.star.script:Standard.Module1.MyMacro?language=Basic&location=document")
   oScript.invoke(array(), array(), array())
   Wait 5000
   oDoc.CurrentController.Frame.ContainerWindow.Visible = True
   oDoc.CurrentController.Frame.ContainerWindow.toFront()
End Sub
Openoffice 4.15
Windows 10
Post Reply