OOoBasic crash course: Working with files

161

Author: Dmitri Popov

OpenOffice.org’s OOoBasic gives users tools to programmatically access and manipulate files. To see how that works, we’ll create a simple macro that allows you to save text snippets from the current document in a plain text file. This macro can be used to store text fragments from multiple documents in one text file, or to save deleted passages in an external file in case you need them later.

To build this macro, you can reuse the component that grabs the selected text fragment in the current document. If you’ve followed the previous installments of the OOoBasic crash course series on creating a lookup macro and working with words, the following code will look familiar:

  Sub WriteFile()
  Dim oDoc As Object, oText As Object
  Dim oCursor As Object
  Dim sFileName As String
  oDoc=ThisComponent
  oText=oDoc.Text
  oCursor=oDoc.CurrentController.getViewCursor
  If oCursor.String ="" then MsgBox "Please select a word to look up" : End

Notice the sFileName variable, defined as string. This variable points to the file for use in the macro. To specify the file you want, use the following code:

  sFileName=ConvertToURL(CurDir) & "/snippets.txt"

The key element here is the ConvertToURL(CurDir) function, which converts the current directory (CurDir) into a URL. While you can use a system-specific path in the macro, converting it into a URL ensures that the path is interpreted correctly on any platform. The CurDir function allows you to conveniently create the snippets.txt file in the current directory. However, what your current directory is depends on the platform you are using. On Windows, opening an existing document sets the current directory to the directory containing the opened file, while the current directory on Linux remains the same no matter what. On my Ubuntu laptop, the current directory is always /usr/lib/openoffice/program, which is not particularly suitable for storing user files. This means that instead of using the CurDir function, you might want to specify the direct path to the text file:

  sFileName=ConvertToURL("/home/user/snippets.txt")

Alternatively, you can skip the ConvertToURL step altogether and hard code the path to the snippets file as a constant:

  Const FileName="C:My Filessnippets.txt"

In order to open the file, you have to specify a unique number, often called “file number” or “data channel.” You can do this easily using the FreeFile function, which obtains a file number you can use in your macro:

  f1=FreeFile()

Next step is to open the file using the Open command:

  Open sFileName For Append Access Read Write As #f1

“For Append” mode creates a new file or opens the existing one, and places the cursor at the end of the file, so you can append new text fragments to it. The Access mode specifies the access privileges; in this case, you can both read the file and modify it. To limit access to the file while you are using it, you can use the Lock mode. This can come in handy if several users use the text file to store their snippets. Use Lock Write to allow the users to view the file but not modify it:

  Open sFileName For Append Access Read Write Lock Write As #f1

Now you can add the code that adds the selected text to the opened file and closes the file:

  Write #f1, oCursor.String
  Close #f1

Here is the final macro:

  Sub WriteFile()
  Dim oDoc As Object, oText As Object
  Dim oCursor As Object
  Dim sFileName As String
  oDoc=ThisComponent
  oText=oDoc.Text
  oCursor=oDoc.CurrentController.getViewCursor
  If oCursor.String ="" then MsgBox "Please select a text fragment" : End
  sFileName=ConvertToURL(CurDir) & "/snippets.txt"
  f1=FreeFile()
  Open sFileName For Append Access Read Write Lock Write As #f1
  Write #f1, oCursor.String
  Close #f1
  MsgBox oCursor.String & "is written to the snippets.txt file"
  End Sub

That’s all there is to it. Now you know how to create a new file, open it, and add selected text to it.

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