Page 1 of 1

Run macro on focus

PostPosted: Thu Sep 12, 2019 3:27 am
by DDeLuca
I have a complicated schedule worksheet.
Some of the people who use it are computer illiterate.
I need it to run a macro that recalculates the times and sorts the spreadsheet when the Windows focus changes to the spreadsheet.
Does anyone know how to do this?

Re: Run macro on focus

PostPosted: Thu Sep 12, 2019 9:48 am
by JeJe
Right click on the sheet tab/sheet events/Activate Document and assign a macro.

Re: Run macro on focus

PostPosted: Fri Sep 13, 2019 12:36 am
by DDeLuca
Thank you JeJe, I didn't know about that function.
Unfortunately that didn't work.
What I meant by changing the Windows focus is using Alt-Tab to a different program and back later. The on events function only works if you are changing from one spreadsheet tab to another.
The people that are using this spreadsheet are only using the one tab and going back and forth from different programs.
I don't even know if OpenOffice Calc can sense the change in Windows program focus.
If I could get Calc to recalculate on the shift in Windows focus, that could be enough.

Re: Run macro on focus

PostPosted: Fri Sep 13, 2019 1:17 am
by JeJe
Try an XTopWindowListener

https://www.openoffice.org/api/docs/com ... tener.html

Put your code where indicated below and run startlistener once only

Code: Select all   Expand viewCollapse view

global count as long
Sub startlistener
  thiscomponent.currentcontroller.frame.containerwindow.addTopWindowListener(CreateUnoListener("WindowListener_", "com.sun.star.awt.XTopWindowListener"))
End Sub
' XTopWindowListener
Sub WindowListener_disposing(ev)
End Sub
Sub WindowListener_windowOpened(ev)
End Sub
Sub WindowListener_windowClosing(ev)
End Sub
Sub WindowListener_windowClosed(ev)
End Sub
Sub WindowListener_windowMinimized(ev)
End Sub
Sub WindowListener_windowNormalized(ev)
End Sub
Sub WindowListener_windowActivated(ev)
'put your code here insead of below two lines which is just to demo
count = count +1
thiscomponent.sheets.getbyindex(0).getcellbyposition(0,0).value=count
End Sub
Sub WindowListener_windowDeactivated(ev)
End Sub


Re: Run macro on focus

PostPosted: Fri Sep 13, 2019 1:28 am
by JeJe
Actually, don't do that.

Go to Tools menu/customise/events/Activate Document and assign to a macro. this one should do the same demo as above

Code: Select all   Expand viewCollapse view
global count
Sub isActivated
count = count +1
thiscomponent.sheets.getbyindex(0).getcellbyposition(0,0).value=count
End Sub


Edit: I've not tested for when exactly the activate document is fired. If this doesn't work as you wan't try the XTopWindowListener then. There may or may not be differences.