One of the major shortcomings of OpenOffice.org Basic (OOo Basic) is the lack of a mechanism that allows you to schedule macros. For example, the Writer's Tools extension comes with the Multi-format Backup tool, which you can use to save backup copies of the currently opened document in different formats. While this tool is useful as it is, adding the ability to create scheduled backups can save you the trouble of manually running the tool every time you want to perform a backup. While it’s not possible to schedule macros using OOo Basic, you can use a simple trick to cheat the system.
With OpenOffice.org, you can execute an OOo Basic macro from a command line. This can be done by running the soffice macro:/// command followed by the path to the macro, which includes library, module, and the macro itself. For example, to run the Multi-format Backup macro on Linux, you can use the following command:
soffice macro:///WriterTools.Backup.MultiFormatBackup
On Windows, you need to switch to the soffice directory first and then run the soffice.exe command:
cd C:\Program Files\OpenOffice.org 2.3\program soffice.exe macro:///WriterTools.Backup.MultiFormatBackup
Knowing this, you can create a scheduled macro by simply writing a simple script containing the required commands and then use cron to run it at predefined time intervals. To do this on Linux, use a text editor to create a new text file containing the following two lines:
#! /bin/bash
soffice macro:///WriterTools.Backup.MultiFormatBackup
Save the file as mfbackup.sh and make it executable using the chmod ugo+x mfbackup.sh command.
On Windows, create a text file with the following commands:
cd C:\Program Files\OpenOffice.org 2.3\program soffice.exe macro:///WriterTools.Backup.MultiFormatBackup
Save the file as mfbackup.bat, and your batch script is ready to go.
The Multi-format Backup tool is designed to take backup of the currently opened Writer document, so you need to modify the macro to avoid errors when OpenOffice.org is not running. This is true not only for the Multi-Format Backup tool, but also for any macro that can run into an error during execution if certain conditions are not met. Basically, you have to modify the macro, so that it aborts execution if an error occurs. To do this, you can use the On Error GoTo statement. In the Multi-format Backup macro, add the following code right before the ThisDoc=ThisComponent line:
On Error GoTo ErrHandler
Comment the following code block using the REM marker:
If ThisDoc.hasLocation=False Then MsgBox ("You must save the document first!", , "Attention!") :End End If
Add then the following two lines before the End Sub code:
Exit Sub ErrHandler:
Finally, you can create a schedule for the created script using cron on Linux. Alternatively, you can use the KCron graphical front-end to cron. If you need help creating a schedule with KCron, take a look at KCron's handbook.
Figure 1: Using KCron to schedule the Multi-format Backup macro
On Windows, you can use Pycron, an easy-to-use graphical task scheduling tool. The following article provides a detailed description of how to use Pycron to create a scheduled task.
Related articles:
| Creating an e-wallet with Base | 2007/08/16 00:27 |
| Creating OpenOffice.org extensions the easy way with BasicAddonBuilder | 2008/02/26 14:06 |
| OpenOffice.org Base primer | 2007/09/23 13:02 |
| Turning OpenOffice.org into a document conversion tool | 2008/01/03 15:09 |
| Digitally sign OpenOffice.org documents | 2007/08/16 00:29 |
| Creating interactive forms with OpenOfice.org Writer | 2007/09/07 09:51 |
| Create high-quality maps with OOoHG | 2007/07/15 18:20 |
| Pepping up OOo Writer documents with sparklines | 2007/09/07 12:46 |
| Scheduling OpenOffice.org Basic macros | 2008/03/20 23:54 |
| WNotes: Creating a Writer/MySQL web application | 2007/09/20 17:25 |
| Scribus for OpenOffice.org users | 2007/11/04 17:05 |