Attach macro to a file?

Hello,

  1. Is it possible to attach a macro to a Calc file?

  2. Can this macro be assigned to a keyboard shortcut?

What I would like to know is if it is possible to attach a specific macro to a Calc file and assign this to a keyboard shortcut, for example Ctrl + T. Then if someone else opens this file on another computer, he can press Ctrl + T and the macro will be executed.

So 2 things have to happen. The macro must be integrated into the Calc file, and it must also be assigned to a keyboard shortcut.

Can that be done, or is it impossible?

Well, LO keyboard bindings aren’t document-specific.

That means that the task would require also a document’s macro that is autoexecuted on the document load, and that would change user’s keyboard bindings. To keep things clean, it would also need another macro to clean up when the document is closed, and even when another document is brought to front. (Not to say that it’s interesting what would happen when an app crash happens that prevents such cleanup).

And, of course, that would mean another possible target for malicious macros.

But for your first part of question - yes, it’s possible to attach a macro to a file, and save it inside a document’s module. To do that, you navigate to the document when create the macro, and create new module inside it.

@mikekaganski Youre right…without some autocustomizing on load you run into some odd Errors!!

Thanks for your answers. So, long story short … what I wanted is not possible and could cause errors. Correct?

In addition to @mikekaganski answer, you can also attach a macro to a toolbar (new or existing) or even a menu for the specific document thus eliminating the need for keyboard bindings.

Edit:

Attached sample has one maco which can be run from the toolbar or menu. The process to create the toolbar or menu entries is best described in the LO documentation. See Getting Started with LibreOffice Chapter 14 - Customizing LibreOffice on this link - click here.

Sample: ToolMenuMacroSample.ods

Could you perhaps give an example? Or explain how this works?

Will add to answer.

Thanks … pretty cool. I’ll look into this.

By the way … do you always have to enable the macro’s when opening the file?

Depending upon where the macro is located, nothing needs to be done. Best to look in macro documentation on same site provided in answer for better clarification.

If this answers your question please click on the :heavy_check_mark: (upper left area of answer).

Alright … but when I clicked your file with the macro it asked me if I wanted to enable the macros. I clicked yes and saved the file. Then when reopening it asked again. There is no way to suppress this behaviour I suppose?

Yes. There are a lot of different things occurring when dealing with macros. It encompasses many, many aspects. This is why I refer to the documentation. Discussion of macros takes on books of information and is not an overnight process. What your specific question deals with is the security. From the menu, Tools->Options. Then under LibreOffice select Security. There on right you probably have Medium selected. Select the Trusted Sources tab & add to Trusted File Locations.

Thank you … that clarifies. I have enough information now to get going. Many thanks for your advice Ratslinger. It’s very much appreciated!

  1. yes →Tools→Makros→…
  2. yes →Tools→Customize→→Keybord……

But you should consider: not any shortcut is available on any OS, some shortcuts are occupied.

if customized file is open but not in focus, there will be an Error after $shortcut