Scheduling OpenOffice.org Basic macros

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:

Better writing with OpenOffice.org and Writer's Tools    
Create ODF documents without OpenOffice.org 2009/03/14 16:24 Dmitri Popov
Create high-quality maps with OOoHG    
Creating OpenOffice.org extensions the easy way with BasicAddonBuilder    
Creating an e-wallet with Base    
Creating interactive forms with OpenOfice.org Writer    
Digitally sign OpenOffice.org documents    
Getting the most out of OpenOffice.org Writer 2009/06/24 22:08 Dmitri Popov
OpenOffice.org Base primer    
Pepping up OOo Writer documents with sparklines    
Scheduling OpenOffice.org Basic macros    
Scribus for OpenOffice.org users    
Turning OpenOffice.org into a document conversion tool    
WNotes: Creating a Writer/MySQL web application    

AddThis Social Bookmark Button

 
articles/schedulemacro.txt · Last modified: 2010/03/10 19:45 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Need high-quality compatible Avery labels? Get them at WorldLabel.
Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki