Run macro on focus

Creating a macro - Writing a Script - Using the API

Run macro on focus

Postby DDeLuca » Thu Sep 12, 2019 3:27 am

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
DDeLuca
 
Posts: 2
Joined: Thu Sep 12, 2019 3:20 am

Re: Run macro on focus

Postby JeJe » Thu Sep 12, 2019 9:48 am

Right click on the sheet tab/sheet events/Activate Document and assign a macro.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 628
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro on focus

Postby DDeLuca » Fri Sep 13, 2019 12:36 am

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
DDeLuca
 
Posts: 2
Joined: Thu Sep 12, 2019 3:20 am

Re: Run macro on focus

Postby JeJe » Fri Sep 13, 2019 1:17 am

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

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 628
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro on focus

Postby JeJe » Fri Sep 13, 2019 1:28 am

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.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 628
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests