August 29, 2006

OOoBasic crash course: Beautifying words and making them smarter

Author: Dmitri Popov

In a previous OOoBasic Crash Course article, we learned how to grab a current word and send it to an external application. Let's see what else we can do with a selected word.

For example, say you have text that contains a lot of technical terms, and you want to link the words that need explaining to their definitions in Wikipedia or some other online reference. Using a simple macro, you can easily add a hyperlink to the currently selected word. To do this, you can use the previous macro as a starting point. In fact, it only needs a few slight modifications.

First of all, you have to specify a new variable to hold the link you want to use. So the first part of the macro should look something like this:

  Sub WikipediaSmartWords
  Dim oDoc As Object, oSmartWord As Object, WikipediaURL As String

Next, you have to instruct the macro to use the currently opened document and assign a value (in this case, it's a link) to the WikipediaURL variable:

  oDoc=ThisComponent
  WikipediaURL="http://en.wikipedia.org/wiki/"

Grabbing the currently selected word is simple:

  oSmartWord=oDoc.CurrentController.getViewCursor

And you are ready to convert the selected word into a smart one:

  oSmartWord.HyperlinkURL=WikipediaURL & oSmartWord.String

Here is what the finished macro looks like:

  Sub WikipediaSmartWords
  Dim oDoc As Object, oSmartWord As Object, WikipediaURL As String
  oDoc=ThisComponent
  WikipediaURL="http://en.wikipedia.org/wiki/"
  oSmartWord=oDoc.CurrentController.getViewCursor
  oSmartWord.HyperlinkURL=WikipediaURL & oSmartWord.String

The smart word looks like a regular link now, but using some OOoBasic magic you can make it stand out from the text crowd. If you take a look at the first line of the macro, you will see that oSmartWord has been defined as an object. OOoBasic objects have a couple of properties that come in quite handy. For example, in the macro, we used the HyperlinkURL property to specify oSmartWord's hyperlink. In a similar manner, you can specify other properties, such as colour, font weight, or underline style. So, to specify the smart word's colour (or colour of the characters, to be precise), use the CharColor property, which is defined as RGB values. If you want smart words to appear in green, the code will look like this:

  oSmartWord.CharColor=RGB(0,125,0)

If you want the smart words to have background colour, specifying the CharBackColor property is the way to go:

  oSmartWord.CharBackColor=RGB(250,250,0)

Similarly, you can specify other character properties, which allow you to beautify your smart words. The table below gives you a couple of properties you can tweak.

Property Type Example What it does
CharStyleName String oSmartWord="Emphasis" Specifies character style
CharFontName String CharFontName="Bitstream Vera Sans" Specifies character font
CharHeight Single CharHeight=5 Defines character height in points
CharShadowed Boolean CharShadowed=True Adds shadow to the character
CharCrossedOut Boolean CharCrossedOut=True Crosses the character
CharContoured Boolean CharContoured=True Makes the character contoured
CharFlash Boolean CharFlash=True Makes the character flash. Avoid this one if you don't want to cause nausea!

If you want smart words to be underlined, you can specify the CharUnderlined property as follows:

  oSmartWord.CharUnderline=com.sun.star.awt.FontUnderline.DOUBLE

As you can see, the value of the CharUnderlined property looks a bit different: com.sun.star.awt.FontUnderlined.DOUBLE, and it's the last part in capitals that actually specifies the underline style. For example, if you prefer to use a wavy line instead of a double line, simply substitute DOUBLE with WAVE:

  oSmartWord.CharUnderline=com.sun.star.awt.FontUnderline.WAVE

Once you've put all the pieces together, the macro should look something like this:

  Sub WikipediaSmartWords
  Dim oDoc As Object, oSmartWord As Object, WikipediaURL As String
  oDoc=ThisComponent
  WikipediaURL="http://en.wikipedia.org/wiki/"
  oSmartWord=oDoc.CurrentController.getViewCursor
  oSmartWord.HyperlinkURL=WikipediaURL & oSmartWord.String
  oSmartWord.CharColor=RGB(0,125,0)
  oSmartWord.CharBackColor=RGB(250, 250, 0)
  oSmartWord.CharUnderline=com.sun.star.awt.FontUnderline.DOUBLE
  End Sub

That's all there is to it. Now you can turn an ordinary word into a smart one and jazz it up a bit.

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

Click Here!