How to compare two text cursor?

In a macro file for a Writer, I browse the text with two text cursor. I wish to compare the position of the two cursors to determine if they are in the same place. I do the following test:

    Dim vCurCursor as Variant
    Dim vWordsCursor As Variant
    Dim vText As Variant
    
	vText = ThisComponent.Text
	
	vCurCursor = vText.CreateTextCursor()
	vCurCursor.GoToStart(False)
	
	vWordsCursor = vText.CreateTextCursor()
	vWordsCursor.GoToStart(False)

... (some code)

        Do
            lCountWord = lCountWord + 1	
            vCurCursor.gotoNextWord(False)
        Loop While NOT (vCurCursor = vWordsCursor)

But it does not work with “(vCurCursor = vWordsCursor)”. The test gives the following error.

Runtime Error BASIC.
Invalid property value.

vCurCursor and vWordsCursor are objects and you need to specify the property of those objects that you want to compare.

Try (vCurCursor.getStart = vWordsCursor.getStart), or use the watch window to see which properties of the two objects would be best for your purpose.

The only way I’ve found to check if two TextCursors are the same is this:

Function areCursorStartsSame( oCur1, oCur2 )
Dim cX
Dim cY

cX = oCur1.getText().createTextCursorByRange(oCur1.getStart())
cY = oCur2.getText().createTextCursorByRange(oCur2.getStart())
cX.gotoRange( cY, true )
If cX.getString() = "" Then
	areCursorStartsSame = true
	Exit Function
End If
areCursorStartsSame = false
End Function

Actually, this function only compares if the cursor starts are the same. (A cursor can be expanded to contain a range of text.)

The way this function works is that it takes the range of text from one cursor to the other and converts it to a string. If the string is empty, it means there’s no text between the two cursors, so they must be the same.