21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Dim Replace As Object<br />

Dim BritishWords(5) As String<br />

Dim USWords(5) As String<br />

BritishWords() = Array("colour", "neighbour", "centre", "behaviour", _<br />

"metre", "through")<br />

USWords() = Array("color", "neighbor", "center", "behavior", _<br />

"meter", "thru")<br />

Doc = ThisComponent<br />

Replace = Doc.createReplaceDescriptor<br />

For I = 0 To 5<br />

Replace.SearchString = BritishWords(I)<br />

Replace.ReplaceString = USWords(I)<br />

Doc.replaceAll(Replace)<br />

Next I<br />

Editing Text Documents<br />

The expressions for searching and replacing are set using the SearchString and ReplaceString properties of<br />

the ReplaceDescriptors. The actual replacement process is finally implemented using the replaceAll<br />

method of the document object, which replaces all occurrences of the search expression.<br />

Example: searching and replacing text with regular expressions<br />

The replacement function of <strong>OpenOffice</strong>.<strong>org</strong> is particularly effective when used in conjunction with regular<br />

expressions. These provide the option of defining a variable search expression with place holders and special<br />

characters rather than a fixed value.<br />

The regular expressions supported by <strong>OpenOffice</strong>.<strong>org</strong> are described in detail in the online help section for<br />

<strong>OpenOffice</strong>.<strong>org</strong>. Here are a few examples:<br />

A period within a search expression stands for any character. The search expression sh.rt therefore can stand<br />

for both for shirt and for short.<br />

The character ^ marks the start of a paragraph. All occurrences of the name Peter that are at the start of a<br />

paragraph can therefore be found using the search expression ^Peter.<br />

The character $ marks a paragraph end. All occurrences of the name Peter that are at the end of a paragraph<br />

can therefore be found using the search expression Peter$.<br />

A * indicates that the preceding character may be repeated any number of times. It can be combined with the<br />

period as a place holder for any character. The temper.*e expression, for example, can stand for the<br />

expressions temperance and temperature.<br />

The following example shows how all empty lines in a text document can be removed with the help of the regular<br />

expression ^$:<br />

Dim Doc As Object<br />

Dim Replace As Object<br />

Dim I As Long<br />

Doc = ThisComponent<br />

Replace = Doc.createReplaceDescriptor<br />

Replace.SearchRegularExpression = True<br />

Replace.SearchString = "^$"<br />

Replace.ReplaceString = ""<br />

Doc.replaceAll(Replace)<br />

More Than Just Text<br />

So far, this chapter has only dealt with text paragraphs and their portions. But text documents may also contain<br />

other objects. These include tables, drawings, text fields and directories. All of these objects can be anchored to<br />

any point within a text.<br />

Thanks to these common features, all of these objects in <strong>OpenOffice</strong>.<strong>org</strong> support a common basic service called<br />

com.sun.star.text.TextContent. This provides the following properties:<br />

Chapter 6 · Text Documents 81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!