Sheet Event Selection Change runs Macro 4 times

Creating a macro - Writing a Script - Using the API

Sheet Event Selection Change runs Macro 4 times

Postby rtellshow » Tue Feb 06, 2018 10:53 pm

I am trying to learn writing macro in OO and find the documentation really frustrating so I turned here for help.

I have a macro that works fine in the Sheet:double click event. When I assign it to the Selection change event it runs 3 to 4 times. To try to debug it I wrote a simple macro that has one line: Msgbox "Selection Change"

Sure enough the msgbox box pops up 4 times for each Selection change event. What am I missing? Is there any online documentation where I can learn?

Thanks very much
OpenOffice 4.1.3 on Windows 10
rtellshow
 
Posts: 9
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Postby Zizi64 » Tue Feb 06, 2018 11:00 pm

OpenOffice 3.2 on Windows XP


- What version are you using really?
- Can you upload your ODF type sample file with the embedded macro code here?
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.5 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.0 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7704
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Sheet Event Selection Change runs Macro 4 times

Postby rtellshow » Tue Feb 06, 2018 11:41 pm

Windows 10
OO 4.1.3

My real macro and test macro exhibit the same behavior: running 4 times for each selection change. So here's is my test macro which is assigned to the selection change event:

Sub Main

Msgbox " Selection Change"

end Sub
OpenOffice 4.1.3 on Windows 10
rtellshow
 
Posts: 9
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Postby Zizi64 » Tue Feb 06, 2018 11:57 pm

Please upload your sample file.
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.5 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.0 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7704
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Sheet Event Selection Change runs Macro 4 times

Postby rtellshow » Wed Feb 07, 2018 12:19 am

Here is the test file. Thanks
Attachments
Test.ods
(9.54 KiB) Downloaded 38 times
OpenOffice 4.1.3 on Windows 10
rtellshow
 
Posts: 9
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Postby Lupp » Wed Feb 07, 2018 1:12 am

As soon as you do the MouseButtonDown the sub starts and displays the message without detaching the mouse movement from the process of selecting. While you try to move the mouse pointer to the OK button, the selection is expanded and a new call to the sub is queued...

It's not a bug, it's a problem.
Well, it may be seen as a bug. But event driven software/programming comes with perils.

You may want to play with my demo which puts the message about the changed selection into the first cell of the selection itself and does not prompt for an OK.

(No. I do not know how to exit the process of selection from the sub's body.)
Attachments
a0092334SelectionChangedDemo_1.ods
(10.18 KiB) Downloaded 32 times
On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Let's create a powerful UFO: United Free Office!
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2310
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Sheet Event Selection Change runs Macro 4 times

Postby rtellshow » Wed Feb 07, 2018 2:58 am

Yes the Mouse is the issue.

To better explain the situation I've modified your Macro which shows the behavior better:
This code grabs the right most character of the AbsoluteName and changes it to an integer. It will then add this value to the value of the selected cell.
if you use the mouse to select a cell, that value will be added 4 times. If you use the up,down keys, the macro runs just once as it should.

This macro assigned to the double click event will work properly with the mouse.

Sub testSelChg(pEvent)
aN = pEvent.AbsoluteName
row = cInt(right(aN,1))
If pEvent.SupportsService("com.sun.star.sheet.SheetCellRanges") Then
rG = pEvent(0)
Else
rG = pEvent
End If

c0 = rG.GetCellByPosition(0,0)
c0.Value =c0.value + row
End Sub
Attachments
SelectionChangedDemo_PutCellRow.ods
(10.17 KiB) Downloaded 35 times
OpenOffice 4.1.3 on Windows 10
rtellshow
 
Posts: 9
Joined: Tue Apr 13, 2010 8:46 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests