Difference between revisions of "NL/Documentation/BASIC Guide/Editing Text Documents"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Created page with "{{NL/Documentation/BASICGuideTOC/v2 |ShowPrevNext=block |ShowPrevPage=block |PrevPage=NL/Documentation/BASIC Guide/Structure of Text Documents |NextPage=NL/Documentation/BASIC...")
 
(De TextCursor)
Line 12: Line 12:
 
== De TextCursor ==
 
== De TextCursor ==
  
Een <tt>TextCursor</tt> in de API van {{OOo}} is te vergelijken met de zichtbare cursor die wordt gebruikt in een document van {{OOo}}. It marks a certain point within a text document and can be navigated in various directions through the use of commands. The <tt>TextCursor</tt> objects available in {{OOo}} Basic should not, however, be confused with the visible cursor. These are two very different things.
+
Een <tt>TextCursor</tt> in de API van {{OOo}} is te vergelijken met de zichtbare cursor die wordt gebruikt in een document van {{OOo}}. Het markeert een bepaald punt binnen een tekst en waar vandaan kan worden genavigeerd in diverse richtingen met behulp van opdrachten. De beschikbare objecten tt>TextCursor</tt> in {{OOo}} BASIC moeten echter niet verward worden met de zichtbare cursor. Dit zijn twee heel verschillende dingen.
  
{{Documentation/VBAnote|Terminology differs from that used in VBA: In terms of scope of function, the Range object from VBA can be compared with the TextCursor object in {{OOo}} and not — as the name possibly suggests — with the Range object in {{OOo}}.}}
+
{{Documentation/VBAnote|Terminologie verschilt van die in VBA gebruikt wordt: In termen van bereik van functie, het object Range van VBA kan worden vergeleken met het object TextCursor  in {{OOo}} en niet – zoals de naam misschien suggereert– met het object Range in {{OOo}}.}}
  
The TextCursor object in {{OOo}}, for example, provides methods for navigating and changing text which are included in the Range object in VBA (for example, MoveStart, MoveEnd, InsertBefore, InsertAfter). The corresponding counterparts of the TextCursor object in {{OOo}} are described in the following sections.
+
Het object TextCursor in {{OOo}}, bijvoorbeeld, verschaft methoden om te navigeren en wijzigen van tekst die zijn opgenomen in het object Range in VBA (bijvoorbeeld, MoveStart, MoveEnd, InsertBefore, InsertAfter). De corresponderende onderdelen van het TextCursor object in {{OOo}} worden beschreven in het volgende gedeelte.
  
=== Navigating within a Text ===
+
=== Navigeren binnen een tekst ===
  
The <tt>TextCursor</tt> object in {{OOo}} Basic acts independently from the visible cursor in a text document. A program-controlled position change of a <tt>TextCursor</tt> object has no impact whatsoever on the visible cursor. Several <tt>TextCursor</tt> objects can even be opened for the same document and used in various positions, which are independent of one another.  
+
Het object <tt>TextCursor</tt> in {{OOo}} BASIC werkt onafhankelijk van de zichtbare cursor in een tekstdocument. Een programma-gecontroleerde positie-verandering van een TextCursor object heeft geen enkel effect op de zichtbare cursor. Diverse objecten <tt>TextCursor</tt> kunnen zelfs geopend zijn voor hetzelfde document en gebruikt op verschillende posities, welke onafhankelijk van elkaar zijn.  
  
A <tt>TextCursor</tt> object is created using the <tt>createTextCursor</tt> call:  
+
Een object <tt>TextCursor</tt> wordt gemaakt met behulp van de aanroep <tt>createTextCursor</tt>:  
  
 
<source lang="oobas">
 
<source lang="oobas">
Line 32: Line 32:
 
</source>  
 
</source>  
  
The Cursor object created in this way supports the <tt>com.sun.star.text.TextCursor</tt> service, which in turn provides a whole range of methods for navigating within text documents. The following example first moves the <tt>TextCursor</tt> ten characters to the left and then three characters to the right:  
+
Het op deze manier gemaakte object Cursor ondersteunt de service <idl>com.sun.star.text.TextCursor</idl>, welke op zijn beurt een groot scala aan methoden voor het navigeren binnen tekstdocumenten verschaft. Het volgende voorbeeld verplaatst eerst de <tt>TextCursor</tt> tien tekens naar links en dan drie tekens naar rechts:
  
<source lang="oobas">Cursor.goLeft(10, False)
+
<source lang="oobas">
Cursor.goRight(3, False)</source>  
+
Cursor.goLeft(10, False)
 +
Cursor.goRight(3, False)
 +
</source>  
  
A <tt>TextCursor</tt> can highlight a complete area. This can be compared with highlighting a point in the text using the mouse. The <tt>False</tt> parameter in the previous function call specifies whether the area passed over with the cursor movement is highlighted. For example, the <tt>TextCursor</tt> in the following example
+
Een <tt>TextCursor</tt> kan een compleet gebied accentueren. Dit kan worden vergeleken met het accentueren van een punt in de tekst, met behulp van de muis. De parameter <tt>False</tt> in de vorige aanroep van de functie specificeert of het gebied waar de cursor zich over verplaatst, wordt geaccentueerd. Bijvoorbeeld, de <tt>TextCursor</tt> in het volgende voorbeeld
  
 
<source lang="oobas">
 
<source lang="oobas">
Line 44: Line 46:
 
</source>  
 
</source>  
  
first moves ten characters to the right without highlighting, and then moves back three characters and highlights this. The area highlighted by the <tt>TextCursor</tt> therefore begins after the seventh character in the text and ends after the tenth character.  
+
verplaatst eerst tien tekens naar links zonder accentuering en gaat dan drie tekens terug en accentueert dat. Het geaccentueerde gebied door de <tt>TextCursor</tt> begint daarom na het zevende teken in de tekst en eindigt na het tiende teken.
  
Here are the central methods that the <idl>com.sun.star.text.TextCursor</idl> service provides for navigation:  
+
Hier zijn de centrale methoden die de service <idl>com.sun.star.text.TextCursor</idl> verschaft voor navigatie:  
  
;<tt>goLeft (Count, Expand)</tt>  
+
;<tt>goLeft (Aantal, Uitgebreid)</tt>  
:jumps Count characters to the left.  
+
:springt Aantal tekens naar links.  
;<tt>goRight (Count, Expand)</tt>  
+
;<tt>goRight (Aantal, Uitgebreid)</tt>  
:jumps Count characters to the right.  
+
:springt Aantal tekens naar rechts.  
;<tt>gotoStart (Expand)</tt>  
+
;<tt>gotoStart (Uitgebreid)</tt>  
:jumps to the start of the text document.  
+
:springt naar het begin van het tekstdocument.  
;<tt>gotoEnd (Expand)</tt>  
+
;<tt>gotoEnd (Uitgebreid)</tt>  
:jumps to the end of the text document.  
+
:springt naar het einde van het tekstdocument.  
;<tt>gotoRange (TextRange, Expand)</tt>  
+
;<tt>gotoRange (TekstBereik, Uitgebreid)</tt>  
:jumps to the specified <tt>TextRange</tt>-Object.  
+
:springt naar het opgegeven object <tt>TekstBereik</tt>.  
;<tt>gotoStartOfWord (Expand)</tt>  
+
;<tt>gotoStartOfWord (Uitgebreid)</tt>  
:jumps to the start of the current word.  
+
:springt naar het begin van het huidige woord.  
;<tt>gotoEndOfWord (Expand)</tt>  
+
;<tt>gotoEndOfWord (Uitgebreid)</tt>  
:jumps to the end of the current word.  
+
:springt naar het einde van het huidige woord.  
;<tt>gotoNextWord (Expand)</tt>  
+
;<tt>gotoNextWord (Uitgebreid)</tt>  
:jumps to the start of the next word.  
+
:springt naar het begin van het volgende woord.  
;<tt>gotoPreviousWord (Expand)</tt>  
+
;<tt>gotoPreviousWord (Uitgebreid)</tt>  
:jumps to the start of the previous word.  
+
:springt naar het begin van het vorige woord.  
 
;<tt>isStartOfWord ()</tt>  
 
;<tt>isStartOfWord ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the start of a word.  
+
:geeft <tt>True</tt> terug als de <tt>TextCursor</tt> aan het begin van een woord staat.  
 
;<tt>isEndOfWord ()</tt>  
 
;<tt>isEndOfWord ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the end of a word.  
+
:geeft <tt>True</tt> terug als de <tt>TextCursor</tt> aan het einde van een woord staat.  
;<tt>gotoStartOfSentence (Expand)</tt>  
+
;<tt>gotoStartOfSentence (Uitgebreid)</tt>  
:jumps to the start of the current sentence.  
+
:springt naar het begin van de huidige zin.  
;<tt>gotoEndOfSentence (Expand)</tt>  
+
;<tt>gotoEndOfSentence (Uitgebreid)</tt>  
:jumps to the end of the current sentence.  
+
:springt naar het einde van de huidige zin.  
;<tt>gotoNextSentence (Expand)</tt>  
+
;<tt>gotoNextSentence (Uitgebreid)</tt>  
:jumps to the start of the next sentence.  
+
:springt naar het begin van de volgende zin.  
;<tt>gotoPreviousSentence (Expand)</tt>  
+
;<tt>gotoPreviousSentence (Uitgebreid)</tt>  
:jumps to the start of the previous sentence.  
+
:springt naar het begin van de vorige zin.  
 
;<tt>isStartOfSentence ()</tt>  
 
;<tt>isStartOfSentence ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the start of a sentence.  
+
:geeft <tt>True</tt> terug als de <tt>TextCursor</tt> aan het begin van een zin staat.  
 
;<tt>isEndOfSentence ()</tt>  
 
;<tt>isEndOfSentence ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the end of a sentence.  
+
:geeft <tt>True</tt> als de <tt>TextCursor</tt> aan het einde van een zin staat.  
;<tt>gotoStartOfParagraph (Expand)</tt>  
+
;<tt>gotoStartOfParagraph (Uitgebreid)</tt>  
:jumps to the start of the current paragraph.  
+
:springt naar het begin van de huidige alinea.  
;<tt>gotoEndOfParagraph (Expand)</tt>  
+
;<tt>gotoEndOfParagraph (Uitgebreid)</tt>  
:jumps to the end of the current paragraph.  
+
:springt naar het einde van de huidige alinea.  
;<tt>gotoNextParagraph (Expand)</tt>  
+
;<tt>gotoNextParagraph (Uitgebreid)</tt>  
:jumps to the start of the next paragraph.  
+
:springt naar het begin van de volgende alinea.  
;<tt>gotoPreviousParagraph (Expand)</tt>  
+
;<tt>gotoPreviousParagraph (Uitgebreid)</tt>  
:jumps to the start of the previous paragraph.  
+
:springt naar het begin van de vorige alinea.  
 
;<tt>isStartOfParagraph ()</tt>  
 
;<tt>isStartOfParagraph ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the start of a paragraph.  
+
:geeft <tt>True</tt> terug als de <tt>TextCursor</tt> aan het begin van een alinea staat.  
 
;<tt>isEndOfParagraph ()</tt>  
 
;<tt>isEndOfParagraph ()</tt>  
:returns <tt>True</tt> if the <tt>TextCursor</tt> is at the end of a paragraph.
+
:geeft <tt>True</tt> terug als de <tt>TextCursor</tt> aan het einde van een alinea staat.
  
 
The text is divided into sentences on the basis of sentence symbols. Periods  are, for example, interpreted as symbols indicating the end of sentences. (In English, at least, they must be followed by a space, tab, or return for this to work.)  
 
The text is divided into sentences on the basis of sentence symbols. Periods  are, for example, interpreted as symbols indicating the end of sentences. (In English, at least, they must be followed by a space, tab, or return for this to work.)  

Revision as of 17:13, 9 February 2013

Book.png


In het vorige gedeelte is al een groot bereik aan opties besproken voor het bewerken van tekstdocumenten, geconcentreerd op de services com.sun.star.text.TextPortion en com.sun.star.text.Paragraph, welke toegang verlenen tot zowel de alinea's als de alinea-gedeelten. Deze services zijn nuttig voor toepassingen waarin de inhoud van een tekst moet worden bewerkt in één doorloop via een lus. Dit is echter voor veel problemen niet voldoende. Apache OpenOffice verschaft de service com.sun.star.text.TextCursor voor meer gecompliceerde taken, inclusief achterwaartse navigatie binnen een document of navigatie gebaseerd op zinnen en woorden in plaats van op TekstDelen.

De TextCursor

Een TextCursor in de API van Apache OpenOffice is te vergelijken met de zichtbare cursor die wordt gebruikt in een document van Apache OpenOffice. Het markeert een bepaald punt binnen een tekst en waar vandaan kan worden genavigeerd in diverse richtingen met behulp van opdrachten. De beschikbare objecten tt>TextCursor</tt> in Apache OpenOffice BASIC moeten echter niet verward worden met de zichtbare cursor. Dit zijn twee heel verschillende dingen.

Documentation note.png VBA : Terminologie verschilt van die in VBA gebruikt wordt: In termen van bereik van functie, het object Range van VBA kan worden vergeleken met het object TextCursor in Apache OpenOffice en niet – zoals de naam misschien suggereert– met het object Range in Apache OpenOffice.


Het object TextCursor in Apache OpenOffice, bijvoorbeeld, verschaft methoden om te navigeren en wijzigen van tekst die zijn opgenomen in het object Range in VBA (bijvoorbeeld, MoveStart, MoveEnd, InsertBefore, InsertAfter). De corresponderende onderdelen van het TextCursor object in Apache OpenOffice worden beschreven in het volgende gedeelte.

Navigeren binnen een tekst

Het object TextCursor in Apache OpenOffice BASIC werkt onafhankelijk van de zichtbare cursor in een tekstdocument. Een programma-gecontroleerde positie-verandering van een TextCursor object heeft geen enkel effect op de zichtbare cursor. Diverse objecten TextCursor kunnen zelfs geopend zijn voor hetzelfde document en gebruikt op verschillende posities, welke onafhankelijk van elkaar zijn.

Een object TextCursor wordt gemaakt met behulp van de aanroep createTextCursor:

Dim Doc As Object
Dim Cursor As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()

Het op deze manier gemaakte object Cursor ondersteunt de service com.sun.star.text.TextCursor, welke op zijn beurt een groot scala aan methoden voor het navigeren binnen tekstdocumenten verschaft. Het volgende voorbeeld verplaatst eerst de TextCursor tien tekens naar links en dan drie tekens naar rechts:

Cursor.goLeft(10, False)
Cursor.goRight(3, False)

Een TextCursor kan een compleet gebied accentueren. Dit kan worden vergeleken met het accentueren van een punt in de tekst, met behulp van de muis. De parameter False in de vorige aanroep van de functie specificeert of het gebied waar de cursor zich over verplaatst, wordt geaccentueerd. Bijvoorbeeld, de TextCursor in het volgende voorbeeld

Cursor.goRight(10, False)
Cursor.goLeft(3, True)

verplaatst eerst tien tekens naar links zonder accentuering en gaat dan drie tekens terug en accentueert dat. Het geaccentueerde gebied door de TextCursor begint daarom na het zevende teken in de tekst en eindigt na het tiende teken.

Hier zijn de centrale methoden die de service com.sun.star.text.TextCursor verschaft voor navigatie:

goLeft (Aantal, Uitgebreid)
springt Aantal tekens naar links.
goRight (Aantal, Uitgebreid)
springt Aantal tekens naar rechts.
gotoStart (Uitgebreid)
springt naar het begin van het tekstdocument.
gotoEnd (Uitgebreid)
springt naar het einde van het tekstdocument.
gotoRange (TekstBereik, Uitgebreid)
springt naar het opgegeven object TekstBereik.
gotoStartOfWord (Uitgebreid)
springt naar het begin van het huidige woord.
gotoEndOfWord (Uitgebreid)
springt naar het einde van het huidige woord.
gotoNextWord (Uitgebreid)
springt naar het begin van het volgende woord.
gotoPreviousWord (Uitgebreid)
springt naar het begin van het vorige woord.
isStartOfWord ()
geeft True terug als de TextCursor aan het begin van een woord staat.
isEndOfWord ()
geeft True terug als de TextCursor aan het einde van een woord staat.
gotoStartOfSentence (Uitgebreid)
springt naar het begin van de huidige zin.
gotoEndOfSentence (Uitgebreid)
springt naar het einde van de huidige zin.
gotoNextSentence (Uitgebreid)
springt naar het begin van de volgende zin.
gotoPreviousSentence (Uitgebreid)
springt naar het begin van de vorige zin.
isStartOfSentence ()
geeft True terug als de TextCursor aan het begin van een zin staat.
isEndOfSentence ()
geeft True als de TextCursor aan het einde van een zin staat.
gotoStartOfParagraph (Uitgebreid)
springt naar het begin van de huidige alinea.
gotoEndOfParagraph (Uitgebreid)
springt naar het einde van de huidige alinea.
gotoNextParagraph (Uitgebreid)
springt naar het begin van de volgende alinea.
gotoPreviousParagraph (Uitgebreid)
springt naar het begin van de vorige alinea.
isStartOfParagraph ()
geeft True terug als de TextCursor aan het begin van een alinea staat.
isEndOfParagraph ()
geeft True terug als de TextCursor aan het einde van een alinea staat.

The text is divided into sentences on the basis of sentence symbols. Periods are, for example, interpreted as symbols indicating the end of sentences. (In English, at least, they must be followed by a space, tab, or return for this to work.)

The Expand parameter is a Boolean value which specifies whether the area passed over during navigation is to be highlighted. All navigation methods furthermore return a Boolean parameter which specifies whether the navigation was successful or whether the action was terminated for lack of text.

The following is a list of several methods for editing highlighted areas using a TextCursor and which also support the com.sun.star.text.TextCursor service:

collapseToStart ()
resets the highlighting and positions the TextCursor at the start of the previously highlighted area.
collapseToEnd ()
resets the highlighting and positions the TextCursor at the end of the previously highlighted area.
isCollapsed ()
returns True if the TextCursor does not cover any highlighting at present.

Formatting Text with TextCursor

The com.sun.star.text.TextCursor service supports all the character and paragraph properties that were presented at the start of this chapter.

The following example shows how these can be used in conjunction with a TextCursor. It passes through a complete document and formats the first word of every sentence in bold type.

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  Cursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

The example first creates a document object for the text that has just been opened. Then it iterates through the entire text, sentence by sentence, and highlights each of the first words and formats this in bold.

Retrieving and Modifying Text Contents

If a TextCursor contains a highlighted area, then this text is available by means of the String property of the TextCursor object. The following example uses the String property to display the first words of a sentence in a message box:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  MsgBox Cursor.String
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

The first word of each sentence can be modified in the same way using the String property:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
 
Do 
  Cursor.gotoEndOfWord(True)
  Cursor.String = "Ups"
  Proceed = Cursor.gotoNextSentence(False)
  Cursor.gotoNextWord(False)
Loop While Proceed

If the TextCursor contains a highlighted area, an assignment to the String property replaces this with the new text. If there is no highlighted area, the text is inserted at the present TextCursor position.

Inserting Control Codes

In some situations, it is not the actual text of a document, but rather its structure that needs modifying. Apache OpenOffice provides control codes for this purpose. These are inserted in the text and influence its structure. The control codes are defined in the com.sun.star.text.ControlCharacter group of constants. The following control codes are available in Apache OpenOffice:

PARAGRAPH_BREAK
paragraph break.
LINE_BREAK
line break within a paragraph.
SOFT_HYPHEN
possible point for syllabification.
HARD_HYPHEN
obligatory point for syllabification.
HARD_SPACE
protected space that is not spread out or compressed in justified text.

To insert the control codes, you need not only the cursor but also the associated text document objects. The following example inserts a paragraph after the 20th character of a text:

Dim Doc As Object   
Dim Cursor As Object
Dim Proceed As Boolean
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor
Cursor.goRight(20, False)
Doc.Text.insertControlCharacter(Cursor, _
    com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)

The False parameter in the call of the insertControlCharacter method ensures that the area currently highlighted by the TextCursor remains after the insert operation. If the True parameter is passed here, then insertControlCharacter replaces the current text.

Searching for Text Portions

In many instances, it is the case that a text is to be searched for a particular term and the corresponding point needs to be edited. All Apache OpenOffice documents provide a special interface for this purpose, and this interface always functions in accordance with the same principle: Before a search process, what is commonly referred to as a SearchDescriptor must first be created. This defines what Apache OpenOffice searches for in a document. A SearchDescriptor is an object which supports the com.sun.star.util. SearchDescriptor service and can be created by means of the createSearchDescriptor method of a document:

Dim SearchDesc As Object
SearchDesc = Doc.createSearchDescriptor

Once the SearchDescriptor has been created, it receives the text to be searched for:

SearchDesc.searchString="any text"

In terms of its function, the SearchDescriptor is best compared with the search dialog from Apache OpenOffice. In a similar way to the search window, the settings needed for a search can be set in the SearchDescriptor object.

The properties are provided by the com.sun.star.util.SearchDescriptor service:

SearchBackwards (Boolean)
searches through the text backward rather than forward.
SearchCaseSensitive (Boolean)
takes uppercase and lowercase characters into consideration during the search.
SearchRegularExpression (Boolean)
treats the search expression like a regular expression.
SearchStyles (Boolean)
searches through the text for the specified paragraph template.
SearchWords (Boolean)
only searches for complete words.

The Apache OpenOffice SearchSimilarity (or “fuzzy match”) function is also available in Apache OpenOffice Basic. With this function, Apache OpenOffice searches for an expression that may be similar to but not exactly the same as the search expression. The number of additional, deleted and modified characters for these expressions can be defined individually. Here are the associated properties of the com.sun.star.util.SearchDescriptor service:

SearchSimilarity (Boolean)
performs a similarity search.
SearchSimilarityAdd (Short)
number of characters which may be added for a similarity search.
SearchSimilarityExchange (Short)
number of characters which may be replaced as part of a similarity search.
SearchSimilarityRemove (Short)
number of characters which may be removed as part of a similarity search.
SearchSimilarityRelax (Boolean)
takes all deviation rules into consideration at the same time for the search expression.

Once the SearchDescriptor has been prepared as requested, it can be applied to the text document. The Apache OpenOffice documents provide the findFirst and findNext methods for this purpose:

Found = Doc.findFirst (SearchDesc)
 
Do Until IsNull(Found)
  ' Edit search results...
  Found = Doc.findNext( Found.End, SearchDesc)
Loop

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

Example: Similarity Search

This example shows how a text can be searched for the word "turnover" and the results formatted in bold type. A similarity search is used so that not only the word “turnover”, but also the plural form "turnovers" and declinations such as "turnover's" are found. The found expressions differ by up to two letters from the search expression:

Dim SearchDesc As Object
Dim Doc As Object
 
Doc = ThisComponent
SearchDesc = Doc.createSearchDescriptor
SearchDesc.SearchString="turnover"
SearchDesc.SearchSimilarity = True
SearchDesc.SearchSimilarityAdd = 2
SearchDesc.SearchSimilarityExchange = 2
SearchDesc.SearchSimilarityRemove = 2
SearchDesc.SearchSimilarityRelax = False
Found = Doc.findFirst (SearchDesc)
 
Do Until IsNull(Found)
  Found.CharWeight = com.sun.star.awt.FontWeight.BOLD
  Found = Doc.findNext( Found.End, SearchDesc)
Loop
Documentation note.png VBA : The basic idea of search and replace in Apache OpenOffice is comparable to that used in VBA. Both interfaces provide you with an object, through which the properties for searching and replacing can be defined. This object is then applied to the required text area in order to perform the action. Whereas the responsible auxiliary object in VBA can be reached through the Find property of the Range object, in Apache OpenOffice Basic it is created by the createSearchDescriptor or createReplaceDescriptor call of the document object. Even the search properties and methods available differ.


As in the old API from Apache OpenOffice, searching and replacing text in the new API is also performed using the document object. Whereas previously there was an object called SearchSettings especially for defining the search options, in the new object searches are now performed using a SearchDescriptor or ReplaceDescriptor object for automatically replacing text. These objects cover not only the options, but also the current search text and, if necessary, the associated text replacement. The descriptor objects are created using the document object, completed in accordance with the relevant requests, and then transferred back to the document object as parameters for the search methods.

Replacing Text Portions

Just as with the search function, the replacement function from Apache OpenOffice is also available in Apache OpenOffice Basic. The two functions are handled identically. A special object which records the parameters for the process is also first needed for a replacement process. It is called a ReplaceDescriptor and supports the com.sun.star.util.ReplaceDescriptor service. All the properties of the SearchDescriptor described in the previous paragraph are also supported by ReplaceDescriptor. For example, during a replacement process, case sensitivity can also be activated and deactivated, and similarity searches can be performed.

The following example demonstrates the use of ReplaceDescriptors for a search within a Apache OpenOffice document.

Dim I As Long
Dim Doc As Object
Dim Replace As Object
Dim BritishWords(5) As String
Dim USWords(5) As String
 
BritishWords() = Array("colour", "neighbour", "centre", "behaviour", _
   "metre", "through")
USWords() = Array("color", "neighbor", "center", "behavior", _
   "meter", "thru")
 
Doc = ThisComponent
Replace = Doc.createReplaceDescriptor
 
For I = 0 To 5
  Replace.SearchString = BritishWords(I)
  Replace.ReplaceString = USWords(I)
  Doc.replaceAll(Replace)
Next I

The expressions for searching and replacing are set using the SearchString and ReplaceString properties of the ReplaceDescriptors. The actual replacement process is finally implemented using the replaceAll method of the document object, which replaces all occurrences of the search expression.

Example: searching and replacing text with regular expressions

The replacement function of Apache OpenOffice is particularly effective when used in conjunction with regular expressions. These provide the option of defining a variable search expression with place holders and special characters rather than a fixed value.

The regular expressions supported by Apache OpenOffice are described in detail in the online help section for Apache OpenOffice. Here are a few examples:

  • A period within a search expression stands for any character. The search expression sh.rt therefore can stand for both for shirt and for short.
  • The character ^ marks the start of a paragraph. All occurrences of the name Peter that are at the start of a paragraph can therefore be found using the search expression ^Peter.
  • The character $ marks a paragraph end. All occurrences of the name Peter that are at the end of a paragraph can therefore be found using the search expression Peter$.
  • A * indicates that the preceding character may be repeated any number of times. It can be combined with the period as a place holder for any character. The temper.*e expression, for example, can stand for the expressions temperance and temperature.

The following example shows how all empty lines in a text document can be removed with the help of the regular expression ^$:

Dim Doc As Object
Dim Replace As Object
Dim I As Long
 
Doc = ThisComponent
Replace = Doc.createReplaceDescriptor
Replace.SearchRegularExpression = True
Replace.SearchString = "^$"
Replace.ReplaceString = ""
 
Doc.replaceAll(Replace)


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools