Macro runs twice

I attached this macro:

Sub setPayPeriodDate
	Dim selection as Integer
	selection = MsgBox("",4 + 128,"Change to the next pay period?")
	
	If selection = 6 then
		Dim timeCard : timeCard = ThisComponent.Sheets.getByName("Chris") 'Note you need to substitute the name of your sheet if you have renamed it.
		Dim dateCell
		Dim currentDate As Date

		' Get current pay period's start date
		dateCell = timeCard.getCellByPosition(0,2)
		currentDate = dateCell.getValue()

		' Change date to beginning of next pay period
		dateCell.setValue(currentDate + 14)
	End If
End Sub

to the OnPrint event. When I initiate the print, the expected Yes/No dialogue pop-up appears. but clicking either button does not close the dialogue box. Clicking a button again closes the dialogue and prints the document. Problem is, clicking the ‘Yes’ button twice, updates the cell twice. Why?

Edited post to properly isolate the code from the post. On another subject, what’s your LO version ?

Hello,
I’ve copied your routine and using a push button attached it to an Execute Action event. Performs exactly as it should. Not having worked with print events before, it leads me to believe that timing is an issue here with the event and your message box. I see nothing wrong with your code.

What’s your LO version ?

Tested with both 4.4.7.2 and 5.1.0.3

I had a similar issue in LO Base. In Base 4.0 Handbook, some events are reported to run twice. I had this problem, when attached an event to a control using code. Not sure why, but following workaround succed.

First check if the control already has an assigned macro to the event you want to trigger from.

If not, assign the event.

If the event is already there, do nothing.

The following code might serve as an example. I’m using Access2Base. But you get the idea. I made a Close button, and assigned the macro from the module itself.

'+++ The following sub is attached to the Open Document Event. Tools -->Customize-->Events
Private Sub MyForm_OpenDocument()
    	Call setControlEvents 
End Sub

Private Sub cmdClose
'+++Simple macro to close a form
	DoCmd.mClose(acForm, "myForm",0)
End Sub

Private Sub setControlEvents
	Dim ocCmdClose As Object
		Set ocCmdClose = Application.Forms("myForm").Controls("myCloseControl")
			
	If Len(ocCmdClose.OnActionPerformed) = 0 Then
           '+++If this event on the control has no macro attached, then do it.
	ocCmdClose.OnActionPerformed = "vnd.sun.star.script:Standard.MyModule.cmdClose?language=Basic&location=document"
	End If
	
	ocCmdClose = Nothing
	
End Sub

After doing this, the macro is called just once per event.

Sorry, but can’t see this as being any kind of solution. In the specified case, a button to close a form, have done this many times & never experienced such a problem. Events which are triggered twice do so for a reason. Most of these are in dealing with records being loaded - into a table control for example. The true solution is to examine the events (using MRI or X-Ray for example) and determine which to adhere to and bypass the unwanted in the macro.

In proceeding with your solution, and provided some of the situations dealt with on forms, the Open Document process could be large and difficult at best to manage when changes in the system were needed. I would avoid this process completely!

Am also getting this problem (firing twice) with Push Buttons, in Base, using Python.

I examined the content.xml of the form in question. This is what some of the code around the button looks like:

<form:button form:name="make_print_version_pb" form:control-implementation="ooo:com.sun.star.form.component.CommandButton" xml:id="control44" form:id="control44" form:label="Make ~print version" office:target-frame="" form:delay-for-repeat="PT0.050000000S" form:image-position="center">

Just wondering about that attribute “form:delay-for-repeat”. Maybe the delay there, apparently 0.05 s, is set too short. Currently trying to find out how to access and modify that attribute using UNO Python.

Essentially the topic:
The document’s OnPrint event is called twice upon successful printing. The Supplement field of the event indicates the print progress according to enum PrintableState: JOB_STARTED and JOB_SPOOLED.

1 Like

Thanks. I’m not sure we’re talking about the same thing. I have a Python embedded macro attached to the “Execute action” event of the button. It is this Python macro which is currently firing twice.

RepeatDelay (see also Repeat).

2 Likes

Have you tried using one of these to see just what is firing off the two events? Can’t recall this happening to me in the past without an error an my part in the code>

1 Like

See tdf#117280 (which is fixed, after removal of the third event, but not documented, hence not closed).

1 Like

@Ratslinger
Yes, thanks. After vast amount of investigation, it turned out that I was putting two identical file handlers on the same logger. Ludicrous.