OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki
OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki
OpenOffice.org BASIC Guide.pdf - OpenOffice.org wiki
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Editing Text Documents 106<br />
"meter", "thru")<br />
Doc = StarDesktop.CurrentComponent<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 />
The expressions for searching and replacing are set using the SearchString and<br />
ReplaceString properties of the ReplaceDescriptors. The actual replacement process is<br />
finally implemented using the replaceAll method of the document object, which replaces all<br />
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<br />
conjunction with regular expressions. These provide the option of defining a variable search<br />
expression with place holders and special 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<br />
help section for <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<br />
sh.rt therefore can stand for both for shirt and for short.<br />
• The character ^ marks the start of a paragraph. All occurrences of the name Peter that<br />
are at the start of a paragraph can therefore be found using the search expression<br />
^Peter.<br />
• The character $ marks a paragraph end. All occurrences of the name Peter that are at<br />
the end of a paragraph 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<br />
be combined with the period as a place holder for any character. The temper.*e<br />
expression, for example, can stand for the expressions temperance and temperature.<br />
The following example shows how all empty lines in a text document can be removed with<br />
the help of the regular expression ^$:<br />
Dim Doc As Object<br />
Dim Replace As Object<br />
Dim I As Long<br />
Doc = StarDesktop.CurrentComponent<br />
Replace = Doc.createReplaceDescriptor<br />
Replace.SearchRegularExpression = True<br />
Replace.SearchString = "^$"<br />
Replace.ReplaceString = ""