This page is one of several trying to help you get the most out of the splendid Open Office. They are allied with a larger set concentrating on ooBase.
Remember that Open Office, including ooBase, is free! But don't let that fool you. And it's not new. Big organizations, government and civilian, are adopting it as their standard office suite... and saving million$, and still Getting The Job Done.
There's more about ooBase in the main index to this material.
This page is "browser friendly". Make your browser window as wide as you want it. The text will flow nicely for you. It is easier to read in a narrow window. With most browsers, pressing plus, minus or zero while the control key (ctrl) is held down will change the texts size. (Enlarge, reduce, restore to default, respectively.) (This is more fully explained, and there's another tip, at my Power Browsing page.)
Page contents © TK Boyd, Sheepdog Software ®, 2/06-3/11.
This tutorial shows how you can put a button on an Open Office page, and arrange for that button to put a "Hello World" message on the screen. I said "page" to be non-specific. We will put the button on an ooWriter document, but it could also go on an ooCalc worksheet, or an ooBase form.
This tutorial was written using ooBase version 3.0 on Windows XP, but many things should work the same way under other post version 1 OpenOffice installations, and under other operating systems. There are some strong reasons to move to Open Office 3.1 if you want to use macros with ooBase. Where the difference is important, you will be told.
I will try... really, I will... to Keep To The Point.
I tend in these tutorials to try to teach you the principles behind all that arises. With those principles, you are far better equipped to adapt what I've presented, to meet your needs. In this essay, however, I will try to keep to the point, and present a "cook-book" answer. But if I do that, will you promise to read the "principles" material I've moved elsewhere? They explain things you need to understand to create and use macros.
For our "Hello World" button, we need a macro. A macro is a recorded sequence of instructions. There are other ways to record one than the one we will use here. Macros can be stored in various places, not just the place I'm going to recommend here. When you know more, you'll be ready to start making your own choices. For now, "trust me"?
We need several pieces in place before everything will work.
The next four sections of this tutorial address each of those in turn.
While what we are going to do can be done in almost the same way on an ooCalc spreadsheet, an ooBase form, an ooImpress presentation, for this tutorial we are going to do it on a "sheet of paper" in ooWriter.
I bet you didn't know that your wordprocessor (ooWriter) could create "computer programs", did you? But as soon as we put our button on the "page", we will have left behind the "word-processor as modern typewriter" world. Don't worry about that. I just mention it, as in a moment I hope you'll find what we're going to do is pretty cool, better than "mere word-processing."
So... open a new text document, using ooWriter. Put "My Button Project" on the page, by simply typing it as if you were doing a letter. Save the document as "OpOf6Button". Let Open Office give it the usual extension (.odt). (The name comes from: "OPen OFfice tutorial example, level 6 (macro work), BUTTON example."). What we are going to do will, of course, work with documents with other names... but there are places where it will help if you use my names for things, at least for the purpose of working through the tutorial.)
That's it! For the document. Now to give it a button....
Use the menu: View | Toolbars | Form Controls. If "Form Controls" doesn't have a tick in front of it, click on it. It may not be docked as below (but you can change that by simple dragging. By the way, don't let the name fool you: The toolbar doesn't let you control (verb) the form. It supplies controls (noun) that you may wish to place on a form you are designing.
You need the button that is second from the left on the Open Office 3.1 Form Controls bar... a set-square and pencil. It will be identified as "Design Mode On/ Off" if you hover the mouse pointer over it.
The "Design Mode On/ Off" item in the toolbar is a toggle... it can be "up" or "down". Clicking it changes your environment between the two possible states:
"Ordinary" ooWriter work is done in the "normal" (design mode off) state. You may, of course, undertake some "design" work during normal operations, e.g. changing the font of your text.... but this is not what OpenOffice is talking about when it speaks of "design mode".
When we are in OpenOffice's design mode, we can add things to the "document" which change it from a mere "piece of paper" with writing and graphics on it. We very rapidly arrive at something which is almost a Windows "program", or "application".
Get yourself into "design mode". The "Design Mode On/ Off" icon will have a border and a background. Many other icons on the toolbar will become "more interesting"... have colors, etc, to show that they are no longer disabled.
Find the "Push Button" icon. (At one point, it was the eighth icon in the illustration above.) Click on it, so that it is "down". Then move your mouse pointer so that it is over the "piece of paper" you have on your screen. Put your mouse button down, move about 3 cm right and 1 cm down... you should see a box "drag out". Release your mouse button, and a button labeled "Button" should be visible on your "piece of paper", which I will henceforth call your "form", the Open Office name for the "thing" we are working with.
If the button is in the wrong place, or the wrong size or shape, you can move or re-size it just by dragging, subject to all the usual frustrations of any new work in any application. The size and placement don't really matter, but most of us want things to be "just so".
Click on the "Design Mode On/ Off" icon to leave design mode... many of the icons in the Form Control window will lose their colors. Try clicking on the button you put on your form. You should see the button go "down" and "up" as you click and release it. Wow! But it's a start.
Save your work in the usual way.
A little preliminary matter: From the menu, invoke Tools | Options. Within the "OpenOffice.org" part of the options, on the "Security" page, you need to click the "Macro Security..." button.
That opens a dialog where you need to set the security to "Medium", so that you can elect, on a document by document basis, to allow macros to run.
Right. That done? Onward.
There are two parts to the rest of our "Make a Hello World" button. First we will create the code which puts "Hello World" on the screen. Then we will "connect" that code to the button.
Most readers will not be starting this work from scratch. Don't be too alarmed about where I am putting the code, if it seems odd to you. If you do things my way, the stuff you do today will be contained in "OpOf6Button.oft", so the rest of your Open Office work won't be "contaminated" by what you've done. If the day comes that you want some macro programming to be pervasive, there are ways to achieve that... and consequences you should by then understand.
- - - - - - - - -
Go to the menu bar of the window with your ooWriter document. Click Tools | Macros | Organize Macros | OpenOffice.org Basic
On the left is a window "Macro from" listing several places macros can be. Click on the "+" in front of our document. (It will be listed as "OpOf6Button" if you saved it with the name I told you to use.) That should reveal a folder called "Standard". Click on "Standard". It should become selected.
Now click the "New" button, to create the shell for a new macro, and name it "OpOf6Button".
You should get the following skeleton....
REM ***** BASIC ***** Sub Main End Sub
Change "Main" to "OpOf6HW" (for "Hello World"), and then copy/paste to the following between "Sub OpOf6HW" and "End Sub"
MsgBox "Hello World"
N.B.: When you type something like that by hand, be sure to put a space after MsgBox
In the macro's window, use File | Save to save what you have, before anything goes wrong. (You haven't done anything "clever"... that "Save" is invoking an ordinary save. Because of how we've done things, you are saving your macro inside your document.)
Just below the menu in the macro editor's window, there's an icon with a green triangle pointing to the right. Its tooltip is "Run BASIC". Click it. A "Hello World" message box should pop up! Ta da!
But we want the button we made to invoke the macro, and it won't.... yet.
Just before we turn to implementing that, let me tell you where I found "MsgBox". In actual fact, I found it the hard way... by pouring over examples in the oooForum.org Macros discussion group. The easy way, the right way to learn the language is to RTFM.... and the manual is to be found at....
... but as you know, "Real Men" (and women) would never do a sensible thing like that. But that's where it is, when you get desperate. The chapter "Language of OpenOffice.org Basic" is particularly helpful.
Well, so far so good. I've Kept To The Point pretty well (for me). At this stage I will not resist a digression. You really need to understand that Open Office programming is object oriented. The button is an "object". As I've said MUCH more about this elsewhere, I won't say much more here.
Objects have properties, and they have event handlers. We'll come back to event handlers in a moment.
You need to learn how to get at any object's properties, and how to set its event handlers. Not too hard, happily, just not particularly obvious.
Use the menu of your ooWriter document to check View | Toolbars. Make sure that the form design toolbar is switched on.
The "Design Mode On/ Off" button on this toolbar is just a duplicate of the one you were using earlier; it isn't different. Make sure your form is in design mode, so that the icons are colored.
There's a "FORM navigator" icon. It is a window with a tiny compass. Fifth icon on the Open Office 3.1 toolbar. (Don't confuse it with the more general "Navigator" (just a compass) icon you may be familiar with.)
Click the Form Navigator icon, and a Form Navigator window should come up, with the elements of your form shown. You may need to click on "+" signs to open the lower branches of the tree. Right-click on the "PushButton" item, click on "Properties", and a quite busy window will open, with two tabs: General and Events. We need "Events". (By the way... you can also access this window by right clicking on the button and clicking "Control", if it is not already ticked. (If it IS ticked, the button's properties window should already be showing somewhere on your screen... as long as you are in the Design mode. But the Form Navigator is useful for seeing the object hierarchies, and for getting at things you can't otherwise easily reach.))
Hopefully, you are now looking at the button's Events... "Before commencing...", etc.
Click on the button to the right of "Mouse button released."
In the window that ensues, click on the "Macro" button below the "Assign:" label, over at the right.
Recognize the things in the "Library" panel? They are what you saw earlier when you created your macro. Click the "+" by "OpOf6Button.odt", and then the "+" by the "Standard" which then appears, and your should see "OpOf6Button" in the "Library" panel. Click on it ( said things were not obvious!), and "OpOf6HW" should finally appear in the "Macro name" panel, both of them selected. Click "OK" to say that you want to assign that macro to the "Mouse button released" event. You also need to "Okay" your way out of the "Assign Action" window. Once you've done that....
Leave "Design Mode" again, and try clicking on the button. The "Hello World" message box should appear. There was always a "mouse button released" event arising when you clicked on the button, but until now, the code (macro) you wrote wasn't assigned as the handler for that event. Now it is. When the event occurs, your macro executes.
Save your document. (I have to admit: Once, mine went a bit "nuts" around now. I'd possibly not "Okayed" out of the "Assign Action" window. Even so, eventually, without changing anything I've told you, things settled down. It just took a few cycles of closing the document, re-opening it, re-doing what I thought I'd done....)
It may seem that the fifty paragraphs or so of this essay have not accomplished very much... but what we have done visits many of the critical elements underlying anything that happens inside a Windows program. You have a few other major issues to master, but not many. Most of the rest of your learning is filling in details. It is as if you've learned most of a foreign language, and now have only to expand your vocabulary.
In the stuff that we've done so far, there are a number of things you may want to know, or need to notice...
*** Suppose you want a particular macro to "fire" each time you open a document? How you go about that is addresses in my tutorial about where macros are stored, in the "appendix" at the bottom, at the moment.
*** There are many places where I've chosen a name for something. In other places, I've allowed OpenOffice's choice to stand. Obviously, I chose the document's name, the macro "folder"'s name, and the macro's name. Less obviously, "Button" is the name of our PushButton. There are other things which have "frozen" names, e.g. "MsgBox" is "built into" the system, is reserved, and cannot be changed.
*** The language is case sensitive. Change "Button" to "button" in some places and the program stops working. There are some things you CAN type in any case, but if in doubt... use the "right" case... especially for names of things. Remember this when you come to use macros in connection with databases. If you want to do something with the table "Status", then you can't refer to it as "status".
*** Be grateful for the "syntax highlighting" that the macro editor provides. The colors help you see what the computer will make of something. Type REM and a space in front of one of the lines. See the color disappear? The REM has turned the line into a REMark... ignored by the system. If "similar" lines don't have similar colors, they are not as similar as you think they are. By the way: Another way to make something a remark: Put an apostrophe in front of it.
Good news/ bad news. When I first wrote this I was using a version 3.0 Open Office installation. Under that version, you couldn't save a macro within an ooBase form. That wasn't not a big deal, though, if you knew the workaround. (Not necessary since Open Office version 3.1) (What you needed to do was to save the macro to an ooWriter document, exactly as described above. When you'd done that, you could set up an ooBase form, put a button on it, and assign the macro as the event handler for a click of the button. You just had to be careful to have the ooWriter document open at any time you are wanting to use the button.) Happily, from Open Office version 3.1 you can save a macro within a database, i.e. within the database's .odb file.
You can skip over this to the general stuff at the bottom of the page, "Editorial philosophy", etc, if you wish. But I thought you deserved a little bonus.
Just before the...
MsgBox "Hello World"
...that is the whole of your macro at the moment, add....
Beep Wait(200) Beep Wait(500) Beep MsgBox "Hello World"
If all is well with the way your sound is set up, you will hear three beeps, separated by intervals of 200 milliseconds and 500 milliseconds. Not rocket science, but maybe fun. If you don't get any sound, my advice is to forget about this. There are just too many things completely outside the operation of the Open Office macro to fool around with. Is your speaker muted? Etc, etc.
I hope you found that informative? If you did, you may find the following tutorials useful, too:
I dislike 'fancy' websites with more concern for a flashy appearance than for good content. For a pretty picture, I can go to an art gallery. Of course, an attractive site WITH content deserves praise... as long as that pretty face doesn't cost download time. In any case....
I am trying to present this material in a format which makes it easy for you to USE it. There are two aspects to that: The way it is split up, and the way it is posted. See the main index to this material for more information about the way it is split up, and the way it is posted.
PLEASE >>> Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages <<< PLEASE
If you liked this ooBase tutorial, see the main index for information other help from the same author.
Editor's email address. Suggestions welcomed! - - - Want a site hosted, or email? I like 1&1's services.
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
. . . . . P a g e . . . E n d s . . . . .