Run macro on focus

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DDeLuca
Posts: 2
Joined: Thu Sep 12, 2019 3:20 am

Run macro on focus

Post 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?
Last edited by MrProgrammer on Thu Sep 12, 2019 4:04 am, edited 1 time in total.
Reason: Moved from Beginners forum to Macros and UNO API
Apache OpenOffice 4.1.6
Windows 7
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro on focus

Post by JeJe »

Right click on the sheet tab/sheet events/Activate Document and assign a macro.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DDeLuca
Posts: 2
Joined: Thu Sep 12, 2019 3:20 am

Re: Run macro on focus

Post 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.
Apache OpenOffice 4.1.6
Windows 7
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro on focus

Post 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


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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro on focus

Post 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

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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply