Linux.com

Home Learn Linux Linux Tutorials Creating Macros Without Scripting in LibreOffice and OpenOffice.org

Creating Macros Without Scripting in LibreOffice and OpenOffice.org

If you find yourself doing the same task over and over again in LibreOffice (or OpenOffice.org), you need to learn about macros. Whether it's inserting the same text over and over, formatting text, or any other task where multiple keystrokes or actions are necessary you can save time by creating a macro. LibreOffice (and OpenOffice.org before it) include an outstanding Macro tool that allows you to quickly create and manage macros that will ease the burden of repetitive tasks.

Many users will see the word "Macro" and want to run screaming or break down weeping at the thought of having to actually "write a macro." We're not programmers! Good news — with LibreOffice (or OpenOffice.org), you don't have to write a single piece of script. The open source office suites have a simple to use Macro recording engine as well as a fine tool to keep all of your macros organized for easy access.

Let's take a look at the process of creating and managing macros with LibreOffice. Though I'm using LibreOffice for the examples, you should be able to do the same thing in OpenOffice.org since LibreOffice is forked off a very recent version of OpenOffice.org and hasn't changed much yet. So if you're using a distro that ships with OO.org by default (most of them, right now) then you don't need to upgrade or switch.

What is a Macro?

Simply stated, a macro is sequence of keystrokes that are recorded and accessed through a simple menu or library tool. When that macro is selected, the recorded keystrokes will automatically execute with no further action from the user. These "macros" can be very simple or quite complex — it all depends upon what you need from the tool. The most important aspects of a macro tool is that it must be powerful and simple to use. LibreOffice macro feature is exactly that. Now, let's dive in.

The macro organizer

LibreOffice Macro Organizer Before we get into the creation of a macro, let's take a look at the tool that is used to organize the LibreOffice macro library. To access this tool click Tools > Macros > Organize Macros > LibreOffice Basic.

When the library opens (see Figure 1) you will see the pre-installed macros in the left navigational tree. You can actually open up each of those macros and edit them. To do that you will have to have a working knowledge of the scripting language used for the LibreOffice macros. For the purpose of this article, we are going to stick with the basics — the macro recording tool. Of course, if you are beyond the basics you can use this window to edit any of the included (or even macros you have created) to your hearts' content.

The primary purpose of this window is to allow you to run any of the given macros or assign any given macro a button on a toolbar or associate a macro to an event. Assigning a macro a button on a toolbar makes for very quick access to your oft-used macros.

Macro Organizer From within this window, if you click the Organizer button the actual organizer will open (see Figure 2). Within this window you can organize all of your macros as well as create new macros and edit existing macros. When creating macros from within the Organizer, you do not have access to the recorder.

Creating macros this way requires the ability to edit the macro scripting language. To give you an Macro editor idea of what the scripting language looks like, take a peek inside of the Userfields macro in Figure 3. Here you see the scripting language as well as the tool used for creating/editing macros without using the recording tool. For the uninitiated it can look a bit overwhelming. Fear not, your introduction to macros does not involve any scripting.

Recording a Macro

Let's dive in shall we? We'll examine this process with a very simple example. We'll have some text already entered into a document and then we'll create a very simple macro to create a letter head for you to use for business correspondence. This macro will:

  1. Center the text at the top of the page.
  2. Write the text for your letterhead.

Once this has been created, we will then add this to a toolbar so all you need to do is click a single button to add your letterhead to your document. Here's how you do it:

Stop recording windowStep 1: The first step is to open up a text document. Once you have that done click Tools > Macros > Record Macro. When you do this a very small window will open (see Figure 4) that will let you know you are recording. This same window is what you click to stop recording your macro.

Step 2: When this window is open begin the process of creating your letter head. You will first click the Center alignment button to center the text. Now type up all of your text for your letter head. Do not try to get fancy as the macro recorder will not record formatting such as bold or font selection.

Step 3: When you have completed the letter head you can click the Stop Recording button. When you do this the Organizer will appear. In this window give your Macro a name (I suggest "Letterhead") and then save it where you want to save it. I suggest saving it under My Macros.

Adding a button to a toolbarStep 4: Now let's add this to a button on the toolbar. Open up the Macro Library and then click the Assign button. When this new window opens click the Add button in the Toolbar Content section. Another new window will open (see Figure 5) which allows you to add a new command to a toolbar. This is a bit tricky. As you can see, in Figure 5, you need to have both of these windows open so you can drag the macro to where you want it to live on the toolbar. Once you've done that you can OK and/or close all of the windows and the new button will appear on your toolbar.

Step 5: Now all you have to do is use the macro button. To do this open up a new document and click the button you just created for your letterhead. The properly aligned and typed text for your letterhead should instantly appear at the top of your page.

Now that is handy!

The Sky is the Limit

Of course what we have done is to create a fairly simple macro. You can create very complex macros with this tool and even create macros for all of the different tools within LibreOffice/OpenOffice.org (spreadsheet, presentations, etc). Now that you have an idea how the macros are created, you can begin to create handy macros of your own that will make your office suite life far easier.

 

Comments

Subscribe to Comments Feed
  • Neom Said:

    Wrong! The macros are very limited in recording ability. You can forget being able to open windows to change formatting. Read the documentation.


Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board