Hello @sgc,
Here’s a method that can highlight ( and unhighlight ) the current paragraph, sentence, and/or word in different colors before and after the cursor:
NB. These highlight colors are set as character background colors ( CharBackColor ) for the different parts of the paragraph, and will remain coloured until you explicitly remove them again.
Sub Writer_HighlightCurrent( iType As Integer, bHighLight As Boolean )
REM Highlights or UnHighlights the current Paragraph, Sentence, and/or Word.
REM Different colors can be specified for each part to be highlighted before and after the cursor ( see Constants below ).
REM <iType> : Integer specifying which parts to Highlight or UnHighlight:
REM 1 = Paragraph ; 2 = Sentence ; 3 = Paragraph & Sentence ; 4 = Word ; 5 = Paragraph & Word ; 6 = Sentence & Word ; 7 = Paragraph & Sentence & Word.
REM <bHighLight> : Boolean indicating whether to set or remove the Highlight for the specified parts.
REM **** Put here the desired colors for the different parts to be highlighted:
Const lParagraphHighlightColorBeforeCursor As Long = &HCCC430
Const lSentenceHighlightColorBeforeCursor As Long = &HDFD400
Const lWordHighlightColorBeforeCursor As Long = &HFFD0AA
Const lWordHighlightColorAfterCursor As Long = &HFFE0BA
Const lSentenceHighlightColorAfterCursor As Long = &HFFF200
Const lParagraphHighlightColorAfterCursor As Long = &HFFF870
Dim oDoc As Object : oDoc = ThisComponent
Dim oSel As Object : oSel = oDoc.CurrentSelection.getByIndex(0)
Dim oViewCursor As Object : oViewCursor = oDoc.CurrentController.getViewCursor()
Dim oTextCursor As Object : oTextCursor = oViewCursor.Text.createTextCursor()
If iType And 1 = 1 Then REM Paragraph
oTextCursor.gotoRange ( oSel, False )
oTextCursor.gotoStartOfParagraph( bHighLight )
If bHighLight Then oTextCursor.CharBackColor = lParagraphHighlightColorBeforeCursor
oTextCursor.gotoEndOfParagraph( True )
If Not bHighLight Then oTextCursor.CharBackTransparent = True
If bHighLight Then oTextCursor.CharBackColor = lParagraphHighlightColorAfterCursor
End If
If iType And 2 = 2 Then REM Sentence
oTextCursor.gotoRange ( oSel, False )
oTextCursor.gotoStartOfSentence( bHighLight )
If bHighLight Then oTextCursor.CharBackColor = lSentenceHighlightColorBeforeCursor
oTextCursor.gotoEndOfSentence( True )
If Not bHighLight Then oTextCursor.CharBackTransparent = True
If bHighLight Then oTextCursor.CharBackColor = lSentenceHighlightColorAfterCursor
End If
If iType And 4 = 4 Then REM Word
oTextCursor.gotoRange ( oSel, False )
oTextCursor.gotoStartOfWord( bHighLight )
If bHighLight Then oTextCursor.CharBackColor = lWordHighlightColorBeforeCursor
oTextCursor.gotoEndOfWord( True )
If Not bHighLight Then oTextCursor.CharBackTransparent = True
If bHighLight Then oTextCursor.CharBackColor = lWordHighlightColorAfterCursor
End If
End Sub
HTH, lib