Book Image

Learn OpenOffice.org Spreadsheet Macro Programming: OOoBasic and Calc automation

By : Dr Mark Alexander Bain
Book Image

Learn OpenOffice.org Spreadsheet Macro Programming: OOoBasic and Calc automation

By: Dr Mark Alexander Bain

Overview of this book

<p>Adding macros to your spreadsheets enables you to add data processing features to your work, automate repetitive tasks, and even create complete data-driven programs that use the spreadsheet as their back end.<br /><br />This book teaches the OOoBasic language and the Calc object model, so that you can manipulate spreadsheets and data from within your programs. You will also see how to create dialog boxes and windows for friendly user interfaces, and how to integrate your spreadsheets with other applications, for example writing spreadsheet data to a document, or capturing data from a database, and using the spreadsheet for generating advanced calculations and reports.<br /><br />Calc is OpenOffice.org's spreadsheet module. Like the rest of OpenOffice.org Calc can be programmed using the built-in language OOoBasic. Both simple macros and complex applications can be developed in this language by controlling Calc through its object model. The book is compatible with the commercial version of OpenOffice.org, StarOffice, and the StarBasic language.</p>
Table of Contents (15 chapters)
Learn OpenOffice.org Spreadsheet Macro Programming
Credits
About the Author
About the Reviewer
Preface

Preface

What would you say if I asked you to name the thing that had the greatest impact on Western Society in the second half of the 20th Century? Chances are you'd say the PC—the ubiquitous Personal Computer. But that's only half the story; it wasn't the PCs themselves that caused the revolution. After all, I got my first PC, a Sinclair ZX 81 back in 1981, and although it made an interesting hobby, it certainly wasn't life changing.

By the end of the 80's I was using something that anyone today would recognize as looking like a PC, but it was still very primitive. Apart from running a word processor called Lex-WP, it was really just an interface to VAX and Unix servers.

So, what was it that turned the PC from just a useful tool into the essential, number one requirement for any business? One answer is Excel—we can even put a date to the start of this revolution—November 1987.

After starting life as Multiplan, Excel became available to everyone who was running Microsoft Windows (and who had the money). Overnight, virtually every major business became addicted to the software; and Microsoft became the giant that we know and love today.

It's not really a surprise that Excel was so successful. It was an application with which you could organize your information to analyze and manipulate your data. You could even extend the basic functionality by using macros.

And that's pretty well how things remained for the rest of the century.

However, things were about to change.

In January 1998, a new term was introduced in a meeting at Palo Alto in California—open source. Then in 2000, Sun Microsystems informed the world that they were going to join the open‑source community; so on 13th October, 2000, OpenOffice.org was born.

Today, the realm of the professional spreadsheet is not just limited to those that can afford it. Today even the smallest business or individual user can use Calc, and (as we'll see in this book) we can take the basic application and bend it to our own will.

Now that's a revolution.

What This Book Covers

Chapter 1 introduces you to the tools that you'll need in order to write your own macros. By the end of the chapter you'll have become acclimatized to Calc's development environment, and you'll know which buttons to press to make your life a little bit easier.

Chapter 2 starts to make use of the basic building blocks that you'll need for your macros: Libraries, Modules, Subroutines, and Functions. By the end of the chapter you'll have your first macro up and running.

Chapter 3 gives an overview of the objects that are built into Calc, and which we can make use of in order to create macros that perform quite complex operations; we'll see just how easy they are to use. We'll also see where to get further information on these objects.

Chapter 4 is where we really get into writing macros. Here you'll learn how to manipulate the contents of one (or more) spreadsheets—and after all, that's what we're here for, isn't it?

Chapter 5 looks at how we can format the data contained in our spreadsheet—it doesn't matter how accurate our data is, if all of the columns overlap each other making the contents impossible to read.

Chapter 6 is an introduction to databases—how to access them, how to display the results of queries in a spreadsheet, and how to change the contents of the databases themselves.

Chapter 7 explains how to make use of other documents (such as charts) within Calc, and how they can be sources of information; for instance, the contents of websites.

Chapter 8 moves away from purely writing code, and shows how you can build a user interface—by building your own dialogs.

Chapter 9 brings everything together. By the end of the chapter you'll be able to create and distribute a complete application.

Chapter 10 takes a look into the future of Calc, and what to do if you're moving from Excel to Calc but don't want to have to rewrite all of your code.

What You Need for This Book

You don’t need to be a programmer to use this book, but you do need to be familiar with the concept of a program and how simple things like a loop might work. The book is compatible with StarBasic, the macro language for commercial version of OOo—StarOffice.

As you progress through the book, you'll find that some of the issues we deal with only relate to the most current versions of OOo. At the time of writing, all of the code in Chapters 2-9 works for version 2.0.4 on Windows and 2.0.2 on Linux. Chapter 10 is another story—that really is on the cutting edge, and for that you'll need Novell's version of OpenOffice.org.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

There are three styles for code. Code words in text are shown as follows: "Don't forget that Main must be the first macro in the module."

A block of code will be set as follows:

Dim fname as String
Dim sname as String
Dim username as String

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

Sub click_cmd_view_symbols
Dim oTxt_company as Object
Dim oLstCompanySymbol as Object
oTxt_company = oFinance_dialog.getControl("txt_company")
oLstCompanySymbol = oFinance_dialog.getControl("lstCompanySymbol")

oLstCompanySymbol.AddItem( oTxt_company.Text ,0)
End Sub

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. To send us general feedback, simply drop an email to, making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visithttp://www.packtpub.com/support and select this book from the list of titles to download any example code or extra resources for this book. The files available for download will then be displayed.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata have been verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.