Writer : How can I set text selection with start and length?

Hello everyone,

I need to select some text from a start position with a length in LibreOffice Writer.

This is for C# but a macro would be helpful too.

Any help will be very appreciated.

Hello @OfficeNet,

To select a range of text within the current Writer document, you could use the following macro:

Sub Writer_selectRange( iStartPos As Integer, iLength As Integer )
REM Select a range of text inside the normal text flow of the current Writer document.
REM <iStartPos> : Short Integer specifying the (0-based) index of the first character to select.
REM <iLength>   : Short Integer specifying the number of characters to select; Pass -1 to select till the end.
REM NB. Textfields within the specified range are counted as a single character, no matter how many characters are inside the Textfield.
	Dim oDoc As Object		  : oDoc	= ThisComponent
	Dim oViewCursor as Object : oViewCursor = oDoc.CurrentController.getViewCursor()
	Dim oTextCursor as Object : oTextCursor = oViewCursor.Text.createTextCursor()
	oTextCursor.gotoStart( False )
	oTextCursor.goRight( iStartPos, False )
	oTextCursor.goRight( iLength, True )
	oDoc.CurrentController.select( oTextCursor )
End Sub

HTH, lib