How to run a Python macro with a form button in a spreadsheet on Windows 10?

Hello!

I’ve beet trying to call a Python macro by choosing: Tools → Run Macro → My Macros → The name → The Function. It doesn’t work. It also doesn’t work when I’m trying to call the macro with a form button, but it work when I choose: Tools → Organize Macros → python → My Macros → The Name → The Function.
As you can see the macro works. Is it possible to call it with a form button on Windows 10? It works on Linux.

Best regards

What makes you think that something does not work?
[ ] Nothing happens?
[ ] Something unexpected happens? (what?)
[ ] Error message? (which one?)
[ ] Application crash?
[ ] Operating system crash?
[ ] Computer exploding?

Hi.

Thank you for the reply.
Nothing happens. Even if I make syntax errors (on purpose), nothing happens - no errors, no messages. If I run the same macro choosing Organise Macros (not Run Macro), it works and I can see the result (some string in a cell). The built-in Python macros act the same.

LibreOffice 7.2.6.2 (x64)
Win 10 Pro

Show your code…

Here you are.

import uno

doc = XSCRIPTCONTEXT.getDocument()

def test():
    activeSheet = doc.getCurrentController().getActiveSheet()
    cell = activeSheet[0,0]
    cell.setString("I'm working.")
    return
1 Like

If you execute directly and from command button, modify you macro…

def test(args=None):
    activeSheet = doc.getCurrentController().getActiveSheet()
    cell = activeSheet[0,0]
    cell.setString("I'm working.")
    return
1 Like

Thank you, but unfortunately it’s not working, as well as any built-in Python marco. I can run them only via Organize Macros submenu. I’ve tried test(args=None), test(*args) and empty argument list and nothing happened. It works on Linux (Manjaro) with no problem, but not on Windows version of LibreOffice.

Does the Windows installation include the Python runtime? It is possible to deselect Python in the office installer whereas the Linux versions typically use the Python which is installed for the entire system.
There should be something like python.exe in the installation directory subdir “program”.

Test document:
t76032.ods (13.7 KB)

Yes, there is that file (python.exe) in C:\Program Files\LibreOffice\program
The buton in your document doesn’t want to call the assigned macro.
Should I uninstall the Python interpreter from LibreOffice or install LibreOffice again without the interpreter?

Just tested using:

Version: 7.2.5.2 (x64) / LibreOffice Community
Build ID: 499f9727c189e6ef3471021d6132d4c694f357e5
CPU threads: 8; OS: Windows 10.0 Build 19044; UI render: default; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: threaded

and aside from needing to grant folder permission to save the python file, all worked without issue.

Great that it works for you, but not for me and that’s the problem here.
I’m gonna handle the problem on my own.
Many thanks to all of you!

@anon39524726
Was only trying to verify Win 10 operation. My Win 10 is fairly vanilla - basically only for testing. Did not have, at the time, any further direction for you to try.
.
Is the macro attached to the button event Execute action?

Yes, it is. But it’s not working even if I try to run it via Tools -> Run Macro submenu. I can run Python macros, even the built-in ones (the examples from LO), only by the Tools -> Organise Macros -> Python Macros submenu.
LO uses its own Python RE not the one I’ve installed on Windows. Probably that’s why I have to add in my macros the path to the modules I’ve installed with pip to use them.

I did not understand or see where that was your environment. Do not have any response for that since I use the LO version on Linux albeit with a different IDE at times…

My uploaded macro document works for me on Linux and on Windows 10 as well. Both systems run the latest LO 7.3.

On Windows Python is built in, so, you are doing something wrong.

Peek 2022-04-03 17-42

without seeing what you do, it is difficult to help you.

1 Like

Hi Stefan and every one interested,

even if it is a old thread, I ran into the same problem, that the macros are not running from either embedded buttons or from the macro selector (Tools->Macro->Run Macro).

My problem was, that even if I closed all LibreOffice files, a instance was still running in the background. After closing that via Task Manager, I was able to run the macro from the macro selector and push button.

Ghostdog, thanks for your replay.

My problem was trivial - I’m an idiot! I wanted to run macros, but I didn’t enable running macros in LibreOffice settings… Such a lame!

Many thanks to all of you!