Sheet Event Selection Change runs Macro 4 times

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
rtellshow
Posts: 15
Joined: Tue Apr 13, 2010 8:46 am

Sheet Event Selection Change runs Macro 4 times

Post by rtellshow »

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
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Sheet Event Selection Change runs Macro 4 times

Post by Zizi64 »

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; 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.
rtellshow
Posts: 15
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Post by rtellshow »

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
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Sheet Event Selection Change runs Macro 4 times

Post by Zizi64 »

Please upload your sample file.
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.
rtellshow
Posts: 15
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Post by rtellshow »

Here is the test file. Thanks
Attachments
Test.ods
(9.54 KiB) Downloaded 200 times
OpenOffice 4.1.3 on Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Sheet Event Selection Change runs Macro 4 times

Post by Lupp »

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 238 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
rtellshow
Posts: 15
Joined: Tue Apr 13, 2010 8:46 am

Re: Sheet Event Selection Change runs Macro 4 times

Post by rtellshow »

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 236 times
OpenOffice 4.1.3 on Windows 10
Post Reply