This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

OOoBasic crash course: Working with files

By Dmitri Popov on October 19, 2006 (8:00:00 AM)

Share    Print    Comments   

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 Files\snippets.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.

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

Share    Print    Comments   

Comments

on OOoBasic crash course: Working with files

Note: Comments are owned by the poster. We are not responsible for their content.

Looks

Posted by: Anonymous Coward on October 20, 2006 12:29 AM
Looks like VBScript / Visual Basic.

#

Re:Looks

Posted by: Anonymous Coward on October 20, 2006 04:37 AM
Hence the presence of "Basic" in the name.

Now, why they didn't just integrate Mono...

#

Re:Looks

Posted by: Anonymous Coward on October 20, 2006 05:05 AM
Unlike MS Office, OpenOffice macros can be written in many languages including Java, Python, StarBasic, and any other language via the UNO bridge mechanism, which allows for some very powerful scripting. C# and Mono are even possible with UNO.

#

Re:Looks

Posted by: Anonymous Coward on October 20, 2006 07:10 PM
It's supposed to, so I can take my megabytes of VBA code for Excel, Access, and Word and run them in OO. Maybe someday I will.

#

scripting OObasic style...

Posted by: Anonymous Coward on October 21, 2006 04:46 AM
I like that it is based on BASIC. Now if only Linux would create a fast modern BASIC COMPILER so development cycle could be faster and we could beat windoze<nobr> <wbr></nobr>... mowaa ha ha >:P

#

Re:scripting OObasic style...

Posted by: Anonymous Coward on October 22, 2006 08:40 AM
Take a look at <a href="http://www.realbasic.com/" title="realbasic.com">REALbasic</a realbasic.com>, <a href="http://gambas.sourceforge.net/" title="sourceforge.net">Gambas</a sourceforge.net>, and <a href="http://hbasic.sourceforge.net/" title="sourceforge.net">HBasic</a sourceforge.net> before you laugh. Three well developed and mature basics for linux. REALbasic is even cross platform between the "big three" OSes.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya