Author: Dmitri Popov
The macro you are about to create uses a standard technique for opening an existing OpenOffice.org file:
ThisDoc = StarDesktop.loadComponentFromURL("path/to/document.odt", "_blank", 0, Array())
The path/to/document.odt
points to the document — for example, home/user/Documents/Loremipsum.odt
on Linux or C:DocumentsLoremipsum.odt
on Windows. The clever part here is that OpenOffice.org can handle not only paths to local files, but also FTP links and Web URLs. So the code that opens a document stored on an FTP server looks something like this:
ThisDoc=StarDesktop.loadComponentFromURL("ftp://username:password@ftp.server.com/Loremipsum.odt", "_blank", 0, Array())
Obviously, a hard-coded FTP link is not very practical, since you’d have to change it manually every time you wanted to open a different document. There are two things you can do to make the macro more flexible. First, you can add an input box that prompts you to enter the name of the document you want to open:
InputMsg="Enter the name of the file" InputTitle="Open document" InputReturn=InputBox (InputMsg, InputTitle)
Next, you can make the macro read the FTP link from a text file. This allows you to quickly modify the FTP link by simply editing it in the text file. Previously we talked about how to write a text selection to a plain text file. Reading from a text file is not that different:
FTPPath=ConvertToURL ("home/user/connectftp.txt") f1=FreeFile() Open FTPPath For Input As #f1
This code block opens the connectftp.txt file for read. The file contains just a single line of text that contains the FTP link (you should replace the FTP login info with the actual account data):
ftp://username:password@ftp.server.com/
Storing your actual account information in a plain text file is a risky thing to do. While securing your data is beyond the scope of this article, one way to protect the text file could be to store it on an encrypted volume.
Finally, you have to modify the open document part of the macro so it can read the FTP link, add the specified file name to it, and open the document:
Do While not Eof(f1)
Line Input #f1, FTPString
ThisDoc = StarDesktop.loadComponentFromURL(FTPString & InputReturn, "_blank", 0, Array())
Loop
Here is what the final macro looks like:
Sub GetFTPDocument()
InputMsg="Enter the name of the file"
InputTitle="Open document"
InputReturn=InputBox (InputMsg, InputTitle)
FTPPath=ConvertToURL ("home/dmpop/connectftp.txt")
f1=FreeFile()
Open FTPPath For Input As #f1
Do While not Eof(f1)
Line Input #f1, FTPString
ThisDoc = StarDesktop.loadComponentFromURL(FTPString & InputReturn, "_blank", 0, Array())
Loop
End Sub
Once the document has been opened, you can edit it and save it back to the FTP server by simply pressing the Save button. With this macro, you don’t have to worry about keeping your documents in several locations in sync, or copying them onto a USB stick when you are on the move. Just move the documents onto your FTP server and open them with a single click whenever and wherever you need them.
Now add versioning
You can stop here, but why not use what you already know to create a macro that saves a version of the current document on the same FTP server? A macro like this provides a no-frills yet efficient versioning tool.
To better understand how this macro works, check the previous OOoBasic crash course article on creating a multi-format backup. The macro starts with finding out the document’s name, then reads an FTP link from the connectftp.txt file, and uploads the document, adding the current date in the ISO format to its name.
Sub SaveFTPDocument()
Dim Prop2(0) As New com.sun.star.beans.PropertyValue
ThisDoc=thisComponent
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
DocURL=ThisDoc.getURL()
DocDir=DirectoryNameoutofPath(DocURL, GetPathSeparator())
FileName=Dir(DocURL, 0)
FTPPath=ConvertToURL ("home/dmpop/connectftp.txt")
f1=FreeFile()
Open FTPPath For Input As #f1
Do While not Eof(f1)
Line Input #f1, FTPString
SaveFTP = FTPString & CDateToISO(Date()) & "-" & FileName
Prop2(0).Name="Overwrite"
Prop2(0).Value=True
ThisDoc.storeToURL(SaveFTP, Prop2())
Loop
End Sub
That’s all there is to it. Now you know how to open a document stored on an FTP server, read data from a text file, and save a version of the current document on an FTP server.
Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, German, and Danish computer magazines.