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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Editing Text Documents<br />

Once the SearchDescriptor has been prepared as requested, it can be applied to the text document. The<br />

<strong>OpenOffice</strong>.<strong>org</strong> documents provide the findFirst and findNext methods for this purpose:<br />

Found = Doc.findFirst (SearchDesc)<br />

Do Until IsNull(Found)<br />

' Edit search results...<br />

Found = Doc.findNext( Found.End, SearchDesc)<br />

Loop<br />

The example finds all matches in a loop and returns a TextRange object, which refers to the found text passage.<br />

Example: Similarity Search<br />

This example shows how a text can be searched for the word "turnover" and the results formatted in bold type. A<br />

similarity search is used so that not only the word “turnover”, but also the plural form "turnovers" and<br />

declinations such as "turnover's" are found. The found expressions differ by up to two letters from the search<br />

expression:<br />

Dim SearchDesc As Object<br />

Dim Doc As Object<br />

Doc = ThisComponent<br />

SearchDesc = Doc.createSearchDescriptor<br />

SearchDesc.SearchString="turnover"<br />

SearchDesc.SearchSimilarity = True<br />

SearchDesc.SearchSimilarityAdd = 2<br />

SearchDesc.SearchSimilarityExchange = 2<br />

SearchDesc.SearchSimilarityRemove = 2<br />

SearchDesc.SearchSimilarityRelax = False<br />

Found = Doc.findFirst (SearchDesc)<br />

Do Until IsNull(Found)<br />

Found.CharWeight = com.sun.star.awt.FontWeight.BOLD<br />

Found = Doc.findNext( Found.End, SearchDesc)<br />

Loop<br />

Note – VBA : The basic idea of search and replace in <strong>OpenOffice</strong>.<strong>org</strong> is comparable to that used in VBA. Both<br />

interfaces provide you with an object, through which the properties for searching and replacing can be defined.<br />

This object is then applied to the required text area in order to perform the action. Whereas the responsible<br />

auxiliary object in VBA can be reached through the Find property of the Range object, in <strong>OpenOffice</strong>.<strong>org</strong> Basic it<br />

is created by the createSearchDescriptor or createReplaceDescriptor call of the document object.<br />

Even the search properties and methods available differ.<br />

As in the old API from <strong>OpenOffice</strong>.<strong>org</strong>, searching and replacing text in the new API is also performed using the<br />

document object. Whereas previously there was an object called SearchSettings especially for defining the<br />

search options, in the new object searches are now performed using a SearchDescriptor or<br />

ReplaceDescriptor object for automatically replacing text. These objects cover not only the options, but also<br />

the current search text and, if necessary, the associated text replacement. The descriptor objects are created using<br />

the document object, completed in accordance with the relevant requests, and then transferred back to the<br />

document object as parameters for the search methods.<br />

Replacing Text Portions<br />

Just as with the search function, the replacement function from <strong>OpenOffice</strong>.<strong>org</strong> is also available in<br />

<strong>OpenOffice</strong>.<strong>org</strong> Basic. The two functions are handled identically. A special object which records the parameters<br />

for the process is also first needed for a replacement process. It is called a ReplaceDescriptor and supports the<br />

com.sun.star.util.ReplaceDescriptor service. All the properties of the SearchDescriptor described in the<br />

previous paragraph are also supported by ReplaceDescriptor. For example, during a replacement process,<br />

case sensitivity can also be activated and deactivated, and similarity searches can be performed.<br />

The following example demonstrates the use of ReplaceDescriptors for a search within a <strong>OpenOffice</strong>.<strong>org</strong><br />

document.<br />

Dim I As Long<br />

Dim Doc As Object<br />

80 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010

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

Saved successfully!

Ooh no, something went wrong!