Outils pour utilisateurs

Outils du site


issue64:tutolibre

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.

J'ai récemment reçu une demande de tutoriel sur les macros LibreOffice ; nous allons donc faire une courte pause dans notre travail dans Présentation pour expliquer brièvement les macros. Les macros vous permettent d'automatiser des actions répétitives telles que taper un en-tête. Cela vous évite d'avoir à taper ou faire le même travail, encore et encore. Dans ce tutoriel, nous verrons comment enregistrer des macros et comment les utiliser. LibreOffice a un langage de script en Basic qui dépasse la portée de ce tutoriel. Peut-être que nous reviendrons aux macros à une date ultérieure pour discuter du langage de script Basic.

REMARQUE : Vous pouvez trouver des informations sur LibreOffice Basic dans l'aide ou télécharger la documentation sur 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.

Activer l'enregistrement de macros

Par défaut, l'enregistrement de macros est désactivé. Apparemment, les macros sont considérées comme une « fonction expérimentale (instable) ». Pour activer l'enregistrement de macros, Outils > Options. Sélectionnez l'option Général sous LibreOffice et cochez la case « Activer les fonctions expérimentales (non stabilisées) ». Cela active l'option « Enregistrer une macro » sous Outils > Macros.

Enregistrer une macro

En mode d'enregistrement de macro, l'enregistreur de macros suit tout ce que vous faites et tout le texte que vous tapez et l'enregistre dans la macro. Vous vous rappelez comment nous avons eu à activer les fonctions expérimentales pour obtenir l'option Enregistrer une macro ? Eh bien, c'est parce que parfois, l'enregistreur de macros se plante. Je ne l'ai pas vécu moi-même, mais je pensais que je devais le souligner.

Comme démonstration d'enregistrement d'une macro, nous allons créer une macro appelée « Signature ». Chaque fois que vous écrivez une lettre, vous devez mettre une formule de politesse, alors pourquoi ne pas en faire une 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.

Commencez avec un nouveau document texte. Outils > Macros > Enregistrer une Macro pour démarrer l'enregistreur de macro. La barre d'outils enregistreur de macro va apparaître. Il n'y a qu'un seul choix sur cette barre d'outils, Terminer l'enregistrement. Appuyez sur la touche Tabulation de votre clavier trois ou quatre fois (ce qui devrait placer le curseur près du centre de la page). Tapez Amicalement, Baisers, ou votre formule favorite. Appuyez sur Entrée deux fois pour laisser place à une signature. Appuyez sur Tabulation autant de fois que vous l'avez fait avant. Mettez le texte en gras en cliquant sur le bouton Gras de la barre d'outils de mise en forme, puis tapez votre nom. Enfin, nous allons ajouter un titre sous le nom. Appuyez sur Entrée, Tabulation le même nombre de fois qu'avant, cliquez sur le bouton Gras pour désactiver le gras, puis cliquez sur le bouton Italique pour passer en italique. Entrez votre titre. Appuyez sur Entrée. Cliquez sur Terminer l'enregistrement.

La boite de dialogue Macros LibreOffice Basic va apparaître. Choisissez le répertoire où vous souhaitez enregistrer votre macro, habituellement Mes macros. Entrez un nom dans le champ Nom de la macro, puis cliquez sur le bouton Enregistrer.

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.

Test de votre macro

Vous devez tester votre macro pour vous assurer que tout est enregistré correctement, Outils > Macros > Exécuter la macro. La boîte de dialogue macro va apparaître. Choisissez le répertoire où vous avez enregistré votre macro, sélectionnez-la, puis cliquez sur le bouton Exécuter. La macro s'exécute en répétant tout le texte que vous avez tapé et le formatage que vous avez fait. Si quelque chose ne vous convient pas, vous pouvez supprimer la macro et en créer une autre. Pour supprimer une macro, Outils > Macros > Gérer les macros > LibreOffice Basic. Trouvez votre macro dans le répertoire, sélectionnez-la et cliquez sur le bouton Supprimer.

Créer un raccourci vers votre macro

Si vous utilisez souvent une macro, vous ne souhaitez sûrement pas aller dans Outils > Macros > Exécuter la macro, à chaque fois que vous en avez besoin. LibreOffice vous permet d'ajouter vos macros à des menus, barres d'outils, raccourcis clavier et événements d'application. Vous pouvez ajouter vos macros dans Outils > Personnaliser.

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.

À titre d'exemple, nous allons ajouter un menu nommé Macros dans Writer et ajouter notre macro Signature à celui-ci. Outils > Personnaliser. Sélectionnez l'onglet Menus. Cliquez sur le bouton Nouveau. Nommez le nouveau menu Macros. Utilisez les flèches pour le déplacer du bas vers la position avant Aide. Cliquez sur OK. Votre nouveau menu est vide pour le moment. Cliquez sur le bouton Ajouter. Sous catégorie, cherchez les Macros LibreOffice et naviguez dans l'arborescence pour trouver votre macro. Sélectionnez la macro Signature et cliquez sur le bouton Ajouter. La macro est ajoutée au menu. Cliquez sur le bouton Fermer. Cliquez sur OK dans la boîte de dialogue Personnaliser. Vous aurez maintenant un élément de menu nommé Macros et, sous lui, la macro Signature. Maintenant, vous pouvez la choisir dans le menu lorsque vous en aurez besoin, ce qui est plus rapide que d'avoir à naviguer jusqu'à Exécuter la macro.

C'était une très brève introduction aux macros. Avant de décider d'utiliser une macro, assurez-vous qu'il n'y a pas une meilleure façon d'accomplir ce que vous essayez de faire, mais pour des actions répétées souvent, les macros pourrait bien être la solution que vous cherchez. Il y a un langage de script LibreOffice Basic et peut-être nous en discuterons à l'avenir. Vous pouvez également télécharger des macros sur le web que vous pouvez importer et utiliser dans LibreOffice.

La prochaine fois, nous reviendrons à Présentation et au travail sur des diapositives.

issue64/tutolibre.txt · Dernière modification : 2012/10/08 23:05 de cqfd93