How to compare SwXTextRange instances

I have an array of SwXTextRanges that I would like to sort by their start position in the document, check for overlaps, and then process.

I checked the XTextRange interface, and I don’t think it allows for something like this. I also checked the fields of the instances in the debugger, and the DocumentIndex sounds like that it could be what I’m looking for, but it’s always Variant/Empty.

Is there any way to actually do this?

XTextRangeCompare (optionally included into Text service).

1 Like

Examples of the use of the methods indicated by @mikekaganski can be found in the famous book by A. Pitonyak OpenOffice.org Macros Explained.odt V4.1.

1 Like

Thanks for pointing me in the right direction. The following code seems to work:

' bubble sort in order of descending start position
For i = 0 To arrCount - 1
  For j = 0 To arrCount - 2 - i
    If ThisComponent.Text.compareRegionStarts(array(j).Start, array(j+1).Start) > 0 Then
      tmp = array(j)
      array(j) = array(j+1)
      array(j+1) = tmp
    EndIf
  Next j
Next i
	
' check for overlap
For i = 0 To arrCount - 2
    If ThisComponent.Text.compareRegionStarts(array(i).Start, array(i+1).End) >= 0 And ThisComponent.Text.compareRegionStarts(array(i+1).Start, array(i).End) >= 0 Then
      MsgBox("Error: found range overlap")
      Exit Sub
    EndIf
Next i

Lifehack.
This forum’s engine gets confused and blushes when it encounters EndIf and some other constructs. Details are here.

1 Like