Page 1 of 1

Sheet Event Selection Change runs Macro 4 times

Posted: Tue Feb 06, 2018 10:53 pm
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

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Tue Feb 06, 2018 11:00 pm
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?

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Tue Feb 06, 2018 11:41 pm
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

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Tue Feb 06, 2018 11:57 pm
by Zizi64
Please upload your sample file.

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Wed Feb 07, 2018 12:19 am
by rtellshow
Here is the test file. Thanks

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Wed Feb 07, 2018 1:12 am
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.)

Re: Sheet Event Selection Change runs Macro 4 times

Posted: Wed Feb 07, 2018 2:58 am
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