AUTHOR'S MAIN SITE   > > > > >   TABLE OF CONTENTS for Open Office database tutorials.   > > > > >   MACROS section, Open Office tutorials.

Open Office Tutorials

Putting multiple macros or subroutines
in one document

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-6/09.



Introduction

This page is one of a group about using macros in Open Office. It is really, really simple compared to the others, but does assume that you know a bit about using macros. If you don't, then I'd suggest that you start with my Introduction to OOP, Events and Macros.



Where to put the code of your macro

One of the things which has kept me from embracing macros is that there is an overhead. You have to manage your macros. They have to be kept where they are accessible... but not so "accessible" that they are underfoot when not needed... or leave "broken bits" in the wake of a not entirely successful experiment.

I'm not entirely happy with my "solution" to the problem, but give it a try. It has merits which may not be immediately evident. I wrote my first computer program in 1968, and have developed some sense of what works, what doesn't. There are many possible solutions, each with pros and cons. What I propose in the following should keep your system manageable, accumulating "pros", and avoiding "cons".

My system is fully explained in the other essays that were listed a the start of this page. The example set out below also uses the system I like.

By the way, this tutorial was written using ooBase version 3.0 on Windows XP, but things should work the same way under other post version 1 OpenOffice installations, and under other operating systems. Great news: From version 3.1, you can store macros in an ooBase database, something you couldn't do before. Open Office upgrades have almost always gone smoothly for me. If you are in a position to take the inevitable risks, I would recommend that you upgrade to version 3.1.


Try this....

You must have enabled macros! To do that, from the main Open Office menu bar, invoke Tools | Options. Within the "OpenOffice.org" part of the options, on the "Security" page, you need to click the "Macro Security..." button, and set the security to "Medium" or lower.

In the first part of this tutorial, we're going to put two buttons on the document, just to highlight something about how the macro debugging environment works.

Start a new ooWriter document.

Make the "Form Controls" toolbar visible. Turn the "Design Mode" "on".

FormControls

Put two buttons on the page. Make their names "buMagenta" and "buCyan", and their labels "Magenta" any "Cyan".

Save your "page"; call it "opof6MultiSub" (OPen OFfice, tutorials section 6 (macro programming), lesson "MULTIple SUBroutines").


The code...

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 "opof6MultiSub" if you saved it with the name I told you to use.) That should reveal a folder called "Standard". Click on that, so that it becomes "selected".

Now click the "New" button, to create a module for some new macro code. Name it "opof6Multi". (You could just leave the name "Module 1", but if you do, and you see that name elsewhere, you can't be sure if it is a different "Module 1", of the one you thought you were working with.)

You should get the following skeleton....

REM  *****  BASIC  *****

Sub Main

End Sub

"Sub" is short for "subroutine", i.e. a chunk of code.

Change the word "Main" to "MakeMagenta". (Unlike some programming languages you may be familiar with, there's no need to have a "Main" subroutine.

Use copy/paste to put the following material between "Sub MakeMagenta" and "End Sub".

Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton.backgroundcolor = &HFF00FF

Before you try to play with that, save what you've got, just in case. You can use the File | Save in the window where you edit the macro code. It merely saves your ooWriter document... the macros, if done as I've suggested, are "inside" that.

Note also: You don't need to close the macro code editing window at any time during this work.

And note, sigh: If you are using OpenOffice 3.1, that should work. If you are using OpenOffice 3.2, change the last part of the....

oForm = oForms.getByName("Standard")

...line to....

oForm = oForms.getByName("Form")

At this time, you may want to arrange the three windows you are currently using.....

... so that you can see "everything" (or at least bits of each, subject to scrolling!) at once: The two buttons, the text you are reading now, and the macro code.

Getting back to working with the macro....

In the macro code editing window, just below the menu bar, here is a pulldown which is described as "Current Library" if you hover your mouse, and to the left of that a stack of paper with a down-pointing blue arrow, and to the left of that a button with a right-pointing green arrow. (Hover your mouse, and that button is described as "run BASIC".)

Click that button now. You should see buMagenta turn magenta.

Change...

oPushButton.backgroundcolor = &HFF00FF

...to...

oPushButton.backgroundcolor = &H0000FF

... and click the "runBasic" button again, and you should see buMagenta turn blue.


A detail...

Don't worry... we're going to take the macro, etc, forward in a moment. But first a little detail....

In....

oPushButton.backgroundcolor = &H0000FF

We are changing the BackGroundColor property of our button. The color is "coded" with a number. If we'd said.....

oPushButton.backgroundcolor = 255

... I bet you'd be quite happy? Try it. It is another way of making the button blue. So what's this "&H0000FF" stuff?

The "&H" bit says that we're going to write a number, but in hex instead of the system we normally use. "System we normally use???" Yes... there are lots of ways to write a number. Twelve is 12 is a dozen, isn't it? Hex is just another way. (12 is written in hex as C !)

But you don't need to know much about hex to do colors.

Red is &HFF0000, Green is &H00FF00, Blue is &H0000FF

In each case, you start with "&H" to say that what's coming is in hex.

Then you have three pairs of characters. Each can be any combination of digits or letter up to F.... so all of the following are okay:

The first pair of characters determines how much red there is in your color... 00 resulting in no red, FF resulting in maximum red. The second pair determines how much green is in your color, and the third pair determines how much blue there is in your color.


Back to the main theme...

At the moment, we can change the color of buMagenta... but when we change the color, we can't change it back without changing the macro code. And the button on the ooWriter document doesn't do anything. We're going to fix both of those problems.

Back to the main theme...

Although it won't, by itself, fix either problem, insert the following in the macro editing window, at the top, just below the "REM *** BASIC ***". Yes- it is very similar to the other code you already have there, and, yes- when you've done this the "runBasic" button no longer runs your original code... it now runs the new subroutine, "MakeCyan".

Sub MakeCyan
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton.backgroundcolor = &H00FFFF
End Sub

Try the "runBasic" button, and buMagenta's color should go cyan


Attaching code to buttons...

Make sure you are in the design mode in the ooWriter window. If you are, right clicking on the button labeled "Cyan" should bring up a menu with "Control" on it. If there is no "tick" in front of it, click it. The menu will go away, but "Control" will now be ticked. When control is ticked, and the document is in design mode, then somewhere on your screen should be a window titled "Properties". As you have just clicked on buCyan, you will be looking at the properties window for buCyan. It has two tabs. You want the "Events" tab.

Click on the button to the right of "Mouse button pressed." (It has "..." in it.)

In the window that ensues, click on the "Macro" button below the "Assign:" label, over to the right.

Recognize the things in the "Library" panel? They are what you saw earlier when you created your macro. Click the "+" by "opof6MultiSub.odt", and then the "+" by the "Standard" which then appears, and your should see "opof6Mulit". Click on that, and you should see MakeCyan and MakeMagenta in the "MacroName" pane, the former being selected. Click "OK" to say that you want to assign the MakeCyan macro to buCyan's Mouse Button Pressed event. Click "Okay" to leave the "Assign Action" window.

With the "Properties" window still open, and your ooWrite document still in design mode, click on buMagenta. The "Properties" window will now be showing information about buMagenta, showing the event handler assignments (none so far) for that button. Repeat (almost) what we did above to assign "MakeMagenta" the event handler for buMagenta's Mouse Button Pressed event.

Now take your ooWriter document out of design mode!

Clicking on buCyan should make buMagenta go cyan, and clicking on buMagenta should make it go... magenta!

In a moment, we'll tweak things so that both buttons change color when either is clicked, but that's beside the point.

The (first) point is that you have a document with more than one macro stored in it, more than one subroutine of ooBasic code which can be invoked by users of your document.

Save what you have!

Now I'm going to show you something "trivial", but the resolution of a mystery you may have noticed.

Use buMagenta, on the ooWriter document, to make the button magenta. Now go to the macro code editing window, and click the "runBasic" button. buMagenta should go cyan. You have a way to quickly test the MakeCyan subroutine. How can we test the MakeMagenta subroutine quickly and easily?

The "runBasic" button runs the subroutine at the top of the window. Use copy/ paste to rearrange things....

REM  *****  BASIC  *****

Sub MakeMagenta
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton.backgroundcolor = &HFF00FF
End Sub

Sub MakeCyan
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton.backgroundcolor = &H00FFFF
End Sub

Click the "runBasic" button, and you should see buMagenta go magenta again. Obviously, you won't want to be endlessly re-ordering the subroutines (macros) in the macro library, but to do some work on a subroutine, you can move it to the top, and use the "runBasic" button for quick testing, until the need arises to change over and work on a different macro.


I've started, so I'll finish...

While everything I wanted to illustrate has already been covered, the way the document's buttons behave is a bit odd. It is a trivial exercise to extend the macros so that both buttons change color when you click one or the other. You might want to try to convert the macros yourself. If you are defeated, the code appears just a little farther down this page.

If the code is opaque to you, you might want to read my tutorial "Miscellaneous thoughts on macros" in which a checkbox was put on an ooWriter page. The code is similar to our button code, but what was happening was examined in greater detail there.

Don't read on, unless you want to see "the answer"!......




Switching the color of two buttons....

REM  *****  BASIC  *****

Sub MakeMagenta
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton,oPushButton2 As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton2 = oForm.getByName("buCyan")
oPushButton.backgroundcolor = &HFF00FF
oPushButton2.backgroundcolor = &HFF00FF
End Sub

Sub MakeCyan
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
Dim oPushButton,oPushButton2 As Object

oThisDoc = thisComponent.getDrawPage()
oForms =oThisDoc.getForms()
oForm = oForms.getByName("Standard")
oPushButton = oForm.getByName("buMagenta")
oPushButton2 = oForm.getByName("buCyan")
oPushButton.backgroundcolor = &H00FFFF
oPushButton2.backgroundcolor = &H00FFFF
End Sub

We've done it!...

That's about it, for this tutorial. I hope it will have made you inclined to return to the main page of my Open Office tutorials, and access another.



Editorial Philosophy

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.


Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

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.




Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


One last bit of advice: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .