Find Text to Add and Remove Text Color Macro

Dear all,

I’ve been struggling with this over the past week and I hope someone can help me out.
I would like to do the following using a macro: I want to seek out all the text with a specific text color and change the color of this text using a macro. In the example below for instance I would like to add a button that would only change the yellow text to black and then another button to go from black back to yellow. I have very little knowledge of coding so any help would be greatly appreciated. I have multiple versions of the same code and I have added the current code below, but none of them work.

Hello and welcome!
The first part of the task is solved very simply - you just need to use the Format button in the Search and Replace window twice to specify the format of the searched text and the format for the replacement text. The code for the resulting macro is shown below.

But the second part of your question will be much more difficult to implement. If you abandon the yellow font color, the text that was highlighted will merge with the rest of the black text. How are you going to remember which part of the text to color back to yellow?

1 Like

Thank you very much for looking into this!
So this is what I’ve tried to do as well before by recording a macro. The macro you have works but then when you change the settings for the Search and Replace and you run the macro, it doesn’t work anymore for some reason.

How I tried to circumvent the problem of the text merging is by making the yellow text not entirely black. So I change it to RGB(0, 0, 1) for instance so that when I want to revert it, I can simply reference that one.

But the problem with the Search and Replace method is that once I have added both a macro for removing and for adding the text color, only one keeps working while the first now just does the same as the second is supposed to do. I think this might have to do with the possibility that the macro only executes the Find and Search command instead of actually filling in the previous parameters.

Rather than mucking about with macros, it would make sense to apply a character style to the relevant portions of text; you can then simply edit the style to achieve changes.

2 Likes

Thank you very much for your time Robleyd!
I am currently looking into this and it seems promissing. I’ll play around with it some more and will come back to this to let you know if I managed to make it work and how I did it.

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

image

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
2 Likes

Thank you both so much for your help. It works now!
All the best!
ResearcherHelp10