Outils pour utilisateurs

Outils du site


issue64:tutolibre

Ceci est une ancienne révision du document !


I recently got a request for a tutorial on LibreOffice macros, so we will take a short break from our work in Impress to briefly cover macros. Macros allow you to automate repetitious actions like typing a letterhead. This frees you from having to type or do the same task over and over again. In this how-to, we will cover how to record macros and use them. LibreOffice has a Basic scripting language which is beyond the scope of this particular how-to. Perhaps we will revisit macros at a later time to discuss the Basic scripting language.

NOTE: You can find information on LibreOffice Basic in the help, or download the documentation at http://wiki.documentfoundation.org/images/d/dd/BasicGuide_OOo3.2.0.odt.

Enabling Macro Recording

By default, macro recording is disabled. Apparently, macros are considered an “experimental (unstable) feature”. To enable macro recording, Tools > Options. Select the General options under LibreOffice, and check “Enable experimental (unstable) features”. This enables the “Record Macro” option under Tools > Macros.

Recording a Macro

When in macro record mode, the macro recorder tracks every action you make and all the text you type, and records it in the macro. Remember how we had to enable experimental features to get the Record Macro option? Well, that's because sometimes the macro recorder does fail. I haven't experienced this myself, but I thought I should point that out.

As a demonstration of recording a macro, we will create a macro called Closing. Every time you write a letter, you have to end with a closing, so why not make it into a macro.

Start with a new text document. Tools > Macros > Record Macro to start the macro recorder. The macro recorder toolbar will show. There is only one choice on this toolbar, Stop Recording. Press the Tab key on your keyboard three or four times (this should place the cursor close to the center of the page). Type Best wishes, love, or your favorite closing. Press Enter twice to leave room for a signature. Press Tab the same number of times you did before. Change the text to bold by clicking the Bold button on the formatting toolbar, and type in your name. Lastly, let's add a title under the name. Press Enter, Tab the same number of times as before, click the Bold button to turn off bold, and click the Italic button to change to italics. Type in your title. Press Enter. Click on Stop Recording.

The Basic Macros dialog box will show. Select the library where you want to save your macro, usually My Macros. Enter a name in the Macro name textbox, and click the Save button.

Testing Your Macro

You will want to test your macro to insure everything recorded correctly, Tools > Macros > Run Macro. The macro dialog box will show. Select the library where you saved your macro, select your macro, and click on the Run button. The macro will run, repeating all the text you typed and the formatting you did. If something didn't turn out just right, you can delete the macro and create a new one. To delete a macro, Tool > Macros > Organize Macros > LibreOffice Basic. Find your macro in the library, select it, and click the Delete button.

Create a Shortcut to your Macro

If you use a macro a lot, you don't want to go to Tools > Macros > Run Macro every time you need to use the macro. LibreOffice allows you to add your macros to menus, toolbars, keyboard shortcuts, and application events. You can add your macros through Tools > Customize.

As an example, let's add a menu named Macros in Writer and add our Closing macro to it. Tools > Customize. Select the Menus tab. Click the New button. Name the new menu Macros. Use the arrow buttons to move it from the bottom to the position before Help. Click OK. Your new menu is empty right now. Click on the Add button. Under category, find LibreOffice Macros and navigate through the tree to find your macro. Select the Closing macro and click the add button. The macro is added to the menu. Click on the Close button. Click OK on the Customize dialog box. You will now have a menu item named Macros, and, under it, the Closing macro. Now, you can select it from the menu when you need it, which is faster than having to navigate to Run Macro.

This has been a very short introduction to macros. Before deciding to use a macro, make sure there isn't a better way to accomplish what you are trying to do, but for often repeated action, macros might just be the solution you are looking for. There is a LibreOffice Basic scripting language, and perhaps we will cover it in the future. You can also download macros from the web that you can import and use in LibreOffice.

Next time, we are back to Impress and working with slides.

issue64/tutolibre.1347569967.txt.gz · Dernière modification : 2012/09/13 22:59 de fredphil91