openoffice-basic

Is there a way to count the number of actual replaces when using replaceAll in OO Basic?


Considering the example for search & replace of specific uk-to-us words from the Editing Text Documents OO Wiki:

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

Question: is there a way to get the count of actual replacement that has been made ? (if any) I don't mind the individual count for each term, but just globally – i.e. if, say, the original text included 2 occurences for 'colour' and 1 for 'behaviour', in the end to get the number 3 (purpose: to report this number to user as info via MsgBox).


Solution

  • As shown in the example at https://www.openoffice.org/api/docs/common/ref/com/sun/star/util/XReplaceable.html, the number found is returned.

    Dim TotalFound As Long
    TotalFound = 0
    ...
      TotalFound = TotalFound + Doc.replaceAll(Replace)
    Next I
    MsgBox "Replaced " & TotalFound & " occurrences"
    

    Result: Replaced 3 occurrences