Let’s take a few steps back from the problem and look at the task more broadly.
So, in a text document there are fragments that are highlighted in color using direct formatting. It is required to temporarily “hide” this formatting and then return it back.
Have you heard of character styles? It’s here - Styles in Writer
If you select a piece of text that is already colored yellow and use this tool

you can create your own character style and name it, for example, myYellow
Now find all the yellow pieces of text and give them this style (just do Find All, close the search window and double click on the style name)
Now you have the opportunity to make changes to the style at any time and change the color of all fragments in the text in one move. Just believe me, the macro that will do this will be much simpler than everything you have done so far.
Sub chngStlColor(sStyleName As String, Optional nColor As Long)
Dim oStyleFamilies As Variant
Dim oCharacterStyles As Variant
Dim oEditedStyle As Variant
oStyleFamilies = ThisComponent.getStyleFamilies()
oCharacterStyles = oStyleFamilies.getByName("CharacterStyles")
oEditedStyle = oCharacterStyles.getByName(sStyleName)
If IsMissing(nColor) Then
oEditedStyle.CharColor = -1
Else
oEditedStyle.CharColor = nColor
EndIf
End Sub
Sub setYelColor
chngStlColor("myYellow", 16776960)
End Sub
Sub clearYelColor
chngStlColor("myYellow")
End Sub