February 28, 2007

OooBasic crash course: One-click email backup of OpenOffice.org documents

Author: Dmitri Popov

Gmail offers a few clever features that make it more than just an email service. You can use your Gmail account as a document viewer, a file storage, and even as a full-blown Getting Things Done solution. You can also turn Gmail into a nifty backup solution for your OpenOffice.org documents using a simple OOoBasic macro and Gmail's own tools.

Of course, OOo offers a Send Document as E-mail feature, but it requires too many steps to be convenient to use. The macro you are about to create allows you to send the currently opened document as an email message, automatically adding information such as the file's name and time stamp, all done in the background without any user interaction. This is, literally, a one-click backup solution for your OpenOffice.org documents.

Before you can actually write the code that sends the document, there are a couple of things you have to take care of. First of all, the macro must check whether the document has a location; a newly created OpenOffice.org document doesn't have a location until it's saved. So the first step is to check whether the document actually exists on the hard disk and then obtain its path:

  ThisDoc=ThisComponent
  If ThisDoc.hasLocation=False Then
  MsgBox "You must save the document first!" :End
  End If
  ThisDocURL=ThisDoc.getURL()

Next, the macro checks whether there are any unsaved changes in the document, and if there are, it saves them:

  If ThisDoc.isModified Then
  ThisDoc.storeAsURL(ThisDocURL, Args)
  End if

To make it easier to manage backups, the subject line of each email message should contain the file name of the document as well as a time stamp. We described how to obtain the name of the current document in a previous article. First, the macro loads the Tools library, which is then used to obtain the file name:

  If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
  GlobalScope.BasicLibraries.LoadLibrary("Tools")
  End If
  DocDir=DirectoryNameoutofPath(ThisDocURL, GetPathSeparator())
  FileName=Dir(ThisDocURL, 0)

The next step is to specify the destination email address and the contents of the subject line:

  MailAddress="backup@email.com"
  MessageSubject="[BACKUP] " & FileName & " " & CDateToISO(Date) & " - " &_
  Hour(Time)& ":" & Minute(Time) & ":" & Second(Time)

To obtain the current date, the macro makes use of the CDateToISO() function, which gets the date in the ISO format. To generate a time stamp, the macro uses three functions: Hour(), Minute(), and Second(). If you plan to use different email addresses to send backups to, then you might want to use an input box instead of a "hard-wired" email address:

  MailAddress=InputBox("Email address")

Now you have everything you need to create a new email message, add attachments to it, and send it. Initiating the email service is easy; it can be done using the following code:

  MailAgent=CreateUnoService("com.sun.star.system.SimpleCommandMail")

or on Windows:

  MailAgent=CreateUnoService("com.sun.star.system.SimpleSystemMail")

To make the macro run on any platform, you need to specify a condition that checks the operating system and uses the appropriate statement. To do this, you can use the GetGUIType function, which returns a numeric value representing the current platform. GetGUIType returns 1 if you are running Windows, 3 for Mac OS X, and 4 for Linux. Using this function, you can create the following condition:

  If GetGUIType=1 Then
  MailAgent=CreateUnoService("com.sun.star.system.SimpleSystemMail")
  Else
  MailAgent=CreateUnoService("com.sun.star.system.SimpleCommandMail")
  End If

Note: On Linux and Mac OS X, you have to explicitly specify the email application that OpenOffice.org uses to send emails by going to Tools -> Options -> Internet -> Email and specifying the desired application.

Finally, the macro creates a new message, specifies the destination address and subject, and adds the current document as an attachment:

  MailClient=MailAgent.querySimpleMailClient()
  MailMessage=MailClient.createSimpleMailMessage()
  MailMessage.setRecipient(MailAddress)
  MailMessage.setSubject(MessageSubject)
  MailMessage.setAttachement(Array(ThisDocURL))

Once this has been done, the macro can send the created message:

  MailClient.sendSimpleMailMessage(MailMessage, 1)

This statement sends the message in the background, but if you prefer to review the message, you can use the 0 option instead:

  MailClient.sendSimpleMailMessage(MailMessage, 0)

And here is the final macro:

  Sub SendMailBackup()
  Dim MailAddress As String, MessageSubject As String
  Dim ThisDocURL As String, DocDir As String, FileName As String
  Dim MailAgent As Object, MailClient As Object, MailMessage As Object, ThisDoc As Object
  Dim Args()

  ThisDoc=ThisComponent
  If ThisDoc.hasLocation=False Then
  MsgBox "You must save the document first!" :End
  End If
  ThisDocURL=ThisDoc.getURL()

  If ThisDoc.isModified Then
  ThisDoc.storeAsURL(ThisDocURL, Args)
  End if

  If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
  GlobalScope.BasicLibraries.LoadLibrary("Tools")
  End If

  DocDir=DirectoryNameoutofPath(ThisDocURL, GetPathSeparator())
  FileName=Dir(ThisDocURL, 0)

  MailAddress="backup@email.com"
  MessageSubject="[OOO_DOC_BACKUP] " & FileName & " " & CDateToISO(Date) & " - " &_
  Hour(Time)& ":" & Minute(Time) & ":" & Second(Time)

  If GetGUIType=1 Then
  MailAgent=CreateUnoService("com.sun.star.system.SimpleSystemMail")
  Else
  MailAgent=CreateUnoService("com.sun.star.system.SimpleCommandMail")
  End If
  MailClient=MailAgent.querySimpleMailClient()
  MailMessage=MailClient.createSimpleMailMessage()

  MailMessage.setRecipient(MailAddress)
  MailMessage.setSubject(MessageSubject)
  MailMessage.setAttachement(Array(ThisDocURL))

  MailClient.sendSimpleMailMessage(MailMessage, 1)
  End Sub
Gmail filter - click to enlarge

When you are done with the macro, you can create a Gmail filter that tags backup emails. Notice that the statement that defines the subject line includes the [OOO_DOC_BACKUP] keyword. This allows you to easily create a Gmail filter. Go to your Gmail account and press the Create a filter link. Type [OOO_DOC_BACKUP] into the Subject field and press the Next button. Tick the Apply the label check box and select an existing label or create a new one (e.g. "backup"). Press the Create Filter button to save the filter. From now on, all backup emails sent from OpenOffice.org using the created macro will be labeled using the specified filter, and you can easily manage them in Gmail.

That's all there is to it. Assign the created macro a keyboard shortcut or create a new menu entry, and your one-click email backup solution is ready to go.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, German, and Danish computer magazines.

Click Here!