[Solved] Change All Instances of one Colour

Discuss the word processor
Post Reply
Dr_Nick
Posts: 12
Joined: Sun Sep 20, 2020 3:53 pm

[Solved] Change All Instances of one Colour

Post by Dr_Nick »

Dear All,

Is there a way to change all instances of one text colour to another? I cannot find anything about this.

I'm trying to learn German and I have been building a word list: I use different colours for each of the three noun genders to help me to remember them.

I decided that other parts of speech (verbs, adjectives, adverbs and so-on and so-forth) should be in red. Well, I clearly didn't understand just how much of the German language is made up of elements other than nouns when I began this endeavour and now the whole thing is a sea of red. Knowing what I do now, I'd have chosen a different colour scheme from the start.

All ideas gratefully received. I'm happy to learn to write macros if I have to.

Kind wishes,

Nick
Last edited by Dr_Nick on Sun Nov 15, 2020 8:24 pm, edited 1 time in total.
OpenOffice4.1.7 Windows 10 (and 7 and Ubuntu)
FJCC
Moderator
Posts: 9273
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Change All Instances of one Colour

Post by FJCC »

I assume you manually set the text color rather than using a character style. If you had used a style, you could edit that and change all of the text at once.

A macro like this may do what you want. The number I chose for the red color may not match what you used when coloring your text. Save a back up copy of your document before running the macro.

Code: Select all

sub Main
oText = ThisComponent.getText()
oParaEnum = oText.createEnumeration()
While oParaEnum.hasMoreElements()
  oPara = oParaEnum.nextElement()
  oPorEnum = oPara.createEnumeration()
  While oPorEnum.hasMoreElements()
    oPor = oPorEnum.nextElement()
    IF oPor.CharColor = 16724787 THEN oPor.CharColor = -1
  WEnd
WEnd
End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Change All Instances of one Colour

Post by John_Ha »

Use the Alternative Find and Replace Extension.

1 open AltFind&Replace
2 place cursor in some coloured text
3 click arrow by Properties and choose last item "Same format of characters (based on cursor). This populates the Search for: box
4 Click Find All. For some reason the first wasn't highlighted - do it manually.
5 While all are highlighted, give them a new colour.

Better still, define a Style called Noun and give them all this style.
Clipboard02.png
Showing that a problem has been solved helps others searching so, if your problem is now solved, please view your first post in this thread and click the Edit button (top right in the post) and add [Solved] in front of the subject.
Last edited by John_Ha on Sun Nov 15, 2020 8:11 pm, edited 1 time in total.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Change All Instances of one Colour

Post by Villeroy »

IMHO, a macro like the one by FJCC would be even more useful if it would change the character style rather than the color.

Code: Select all

    IF oPor.CharColor = 16724787 THEN oPor.CharStyleName = "German"
where "German" is an existing unserdefined character style with color, language setting and any other attribut you want to assign to this type of text snippet.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Dr_Nick
Posts: 12
Joined: Sun Sep 20, 2020 3:53 pm

Re: Change All Instances of one Colour

Post by Dr_Nick »

Thank you all, this is all very helpful.

I am very grateful to you for taking the time to help.

Kind wishes,

Nick
OpenOffice4.1.7 Windows 10 (and 7 and Ubuntu)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Change All Instances of one Colour

Post by Villeroy »

The tool introduced by John_Ha can select text portions by attributes. Let it select all the red text and then double-click your userdefined character style in order to apply the style to the current selection.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: [Solved] Change All Instances of one Colour

Post by Bill »

The standard Find & Replace will work in OpenOffice Writer, but the user has to click More Options > Format and manually select the font color. After colors are selected for "Search for" and "Replace with", leave both "Search for" and "Replace with" blank and click Replace All to change all instances.
AOO 4.1.14 on Ubuntu MATE 22.04
Dr_Nick
Posts: 12
Joined: Sun Sep 20, 2020 3:53 pm

Re: [Solved] Change All Instances of one Colour

Post by Dr_Nick »

Thank you,

I didn't know that this feature existed.

So far I haven't worked out how to set the "Search for" and the "Replace with" colours, but I shall keep playing with it.

Kind wishes,

Nick
OpenOffice4.1.7 Windows 10 (and 7 and Ubuntu)
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: Change All Instances of one Colour

Post by Minhoca »

John_Ha wrote: Sun Nov 15, 2020 8:05 pm Use the Alternative Find and Replace Extension.

1 open AltFind&Replace
2 place cursor in some coloured text
3 click arrow by Properties and choose last item "Same format of characters (based on cursor). This populates the Search for: box
4 Click Find All. For some reason the first wasn't highlighted - do it manually.
5 While all are highlighted, give them a new colour.

Better still, define a Style called Noun and give them all this style.

Clipboard02.png
Showing that a problem has been solved helps others searching so, if your problem is now solved, please view your first post in this thread and click the Edit button (top right in the post) and add [Solved] in front of the subject.
Hey, I have been attempting to do this same thing but with the background color of the text, it worked well, however I can't find the HEX or RGB value of the color I picked anywhere in the properties of the text, and the number shown in the dialog box in AltSearch doesn't seem to match either of those things. How do I find the exact value for the color so I can more easily change it later (I also want to make this change on another document).

I also wanted to try this on the background colors of the tables inside my document, would that be possible?
Apache OpenOffice 4.1.7
Windows 10 Pro
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Change All Instances of one Colour

Post by JeJe »

If you chose your colors from the palette you can find the values in Tools menu/options/colors

Edit:or a simple macro like this for the back colors at the cursor:

Code: Select all

Sub MsgboxColors
msgbox "charbackcolor: " & thiscomponent.currentcontroller.viewcursor.charbackcolor & chr(10) & "parabackcolor: " & thiscomponent.currentcontroller.viewcursor.parabackcolor 
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

JeJe wrote: Mon Sep 18, 2023 1:57 am If you chose your colors from the palette you can find the values in Tools menu/options/colors

Edit:or a simple macro like this for the back colors at the cursor:

Code: Select all

Sub MsgboxColors
msgbox "charbackcolor: " & thiscomponent.currentcontroller.viewcursor.charbackcolor & chr(10) & "parabackcolor: " & thiscomponent.currentcontroller.viewcursor.parabackcolor 
End Sub
Well that is the thing, I did choose it from the palette, but NOT OOW's one. I believe I made this document in LibreOfficeWriter (or maybe an older version of OOW, or some other Writer program) then switched to OOW when I changed computers, and now I can't find the original colors I used in the palette. So I wanted the HEX values for the colors I used to make it easier to find all tables with that same color with AltSearch, as there are a lot of them, and change them all into colors from OOW palette as they are in my opinion a lot prettier.

As for that Macro, I never really used macros so I will have to find out how to make use of it, but I will give that a shot.
Apache OpenOffice 4.1.7
Windows 10 Pro
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Change All Instances of one Colour

Post by JeJe »

[Tutorial] How to install a code snippet:

viewtopic.php?t=5519

You don't need to know the existing color with this macro.
Put the text cursor in a table with the backcolor you want to change and run it.
An input box will ask you for the new colour - put something like &H5678 for a hex number in it.
All the tables with the matching backcolor will be changed to the one you entered.

Code: Select all

Sub ChangeTableBackColors
col1 =thiscomponent.currentcontroller.viewcursor.texttable.backcolor
res = inputbox("Input hex value for color")
if res <>"" then 

tbls = thiscomponent.texttables
for i = 0 to tbls.count -1
if tbls(i).BackColor = col1 then tbls(i).BackColor = res
next
end if
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Change All Instances of one Colour

Post by JeJe »

Slightly better macro, instead of showing an input box that requires text entry, shows the table dialog
- if you change the table backcolor with it then all the matching colored tables will be changed too.

Code: Select all

sub ChangeTableBackColors2
dim document   as object
dim dispatcher as object
vc = thiscomponent.currentcontroller.viewcursor
if isempty(vc.texttable) = false then
col1 =vc.texttable.backcolor
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:TableDialog", "", 0, array)
col2 =vc.texttable.backcolor
if col1 <> col2 then
tbls = thiscomponent.texttables
for i = 0 to tbls.count -1
if tbls(i).BackColor = col1 then tbls(i).BackColor = col2
next
end if
end if
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

JeJe wrote: Tue Sep 19, 2023 12:38 am Slightly better macro, instead of showing an input box that requires text entry, shows the table dialog
- if you change the table backcolor with it then all the matching colored tables will be changed too.

Code: Select all

sub ChangeTableBackColors2
dim document   as object
dim dispatcher as object
vc = thiscomponent.currentcontroller.viewcursor
if isempty(vc.texttable) = false then
col1 =vc.texttable.backcolor
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:TableDialog", "", 0, array)
col2 =vc.texttable.backcolor
if col1 <> col2 then
tbls = thiscomponent.texttables
for i = 0 to tbls.count -1
if tbls(i).BackColor = col1 then tbls(i).BackColor = col2
next
end if
end if
end sub
Sorry the late reply, currently trying this. I wasn't able to rename the Module1 to a more fitting name as stated in one of the steps of the code snippet tutorial linked. For some reason right-clicking it doesn't do anything and I am only able to name new folders and the macros themselves.

When I tried running the macro, it just didn't do anything, not sure what I am doing it. I assumed it was going to pop-up a little window asking for the color to be changed and the color to overwrite it, but nothing. I tried selecting some text or everything with Ctrl + A, but also nothing. I tried selecting a random table in my document, strangely running the macro would prompt the Table... window, but it would remain unchanged from the regular one found by right-clicking the table, even as far as being at the same tab I left it the last time (I tried using that window but it didn't do anything unexpected from the regular Table... window) I opened it normally.

I tried following the steps in the code-snippet tutorial linked, but perhaps I made a mistake.
Apache OpenOffice 4.1.7
Windows 10 Pro
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

I tried the first code snippet too, it does open the window but after I enter the HEX color and press "OK" it doesn`t do anything, regardless of where my cursor is. I tried all variations like "&H5678 FF00E8", "FF00E8", "#FF00E8", "&H5678#FF00E8", "&H5678FF00E8" inside the text box and none of them seemed to work.
Apache OpenOffice 4.1.7
Windows 10 Pro
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Change All Instances of one Colour

Post by JeJe »

did you try putting in just
&H5678
as in the post. No quote marks. no following number for some reason you added a second number after it in your ones you tried.


*

For the second macro. You put the cursor in a table and run and up pops the normal table dialog you switch to the backcolor tab and change the backcolor as required then click on okay. That will change the backcolor of that table - but the macro will also run through all the other tables of the document which were the same color as that table and change them to the new color too. That's what I thought you were trying to do.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: [Solved] Change All Instances of one Colour

Post by Bill »

Minhoca wrote: Mon Sep 18, 2023 3:43 pm I believe I made this document in LibreOfficeWriter (or maybe an older version of OOW, or some other Writer program) then switched to OOW when I changed computers, and now I can't find the original colors I used in the palette. So I wanted the HEX values for the colors I used to make it easier to find all tables with that same color with AltSearch, as there are a lot of them, and change them all into colors from OOW palette as they are in my opinion a lot prettier.
There are many color picker utilities that can be installed in Windows that will get the HEX values, or install LO and use the "Custom color..." feature which will also give you the HEX values.
AOO 4.1.14 on Ubuntu MATE 22.04
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: Change All Instances of one Colour

Post by Bill »

Minhoca wrote: Mon Sep 18, 2023 1:04 am ...I can't find the HEX or RGB value of the color I picked anywhere in the properties of the text, and the number shown in the dialog box in AltSearch doesn't seem to match either of those things. How do I find the exact value for the color so I can more easily change it later (I also want to make this change on another document).ble?
The number in the dialog box on AltSearch is the HEX value converted to decimal. Convert it to hexadecimal to get the HEX value.
AOO 4.1.14 on Ubuntu MATE 22.04
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: Change All Instances of one Colour

Post by Bill »

Minhoca wrote: Mon Sep 18, 2023 1:04 am I also wanted to try this on the background colors of the tables inside my document, would that be possible?
I don't think that's possible using AltSearch. The properties settings in AltSearch are based on character format. I don't see any settings based on table format.
AOO 4.1.14 on Ubuntu MATE 22.04
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Change All Instances of one Colour

Post by JeJe »

Bill wrote: Sun Sep 24, 2023 1:39 pm
Minhoca wrote: Mon Sep 18, 2023 1:04 am I also wanted to try this on the background colors of the tables inside my document, would that be possible?
I don't think that's possible using AltSearch. The properties settings in AltSearch are based on character format. I don't see any settings based on table format.
That's the part of the problem my macros were intending to solve.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

JeJe wrote: Sun Sep 24, 2023 10:27 am did you try putting in just
&H5678
as in the post. No quote marks. no following number for some reason you added a second number after it in your ones you tried.


*

For the second macro. You put the cursor in a table and run and up pops the normal table dialog you switch to the backcolor tab and change the backcolor as required then click on okay. That will change the backcolor of that table - but the macro will also run through all the other tables of the document which were the same color as that table and change them to the new color too. That's what I thought you were trying to do.
Yeah I tried, both with the text cursor inside the table as well as selecting the whole table (having it completely blueish selected). Nothing happened in either case. I wasn't sure if the &H5678 was the color itself or a code that made it accept HEX codes later, hence why I tried following it with a HEX color afterwards. Not sure why I forgot to mention I tried just &H5678 in the earlier reply.

UPDATE: Everything is working now. I realised my mistake, there is a difference between TABLE BACKGROUND COLOR and BACKGROUND (everything?) COLOR. There is a paint bucket tool on the top of the screw that paints the whole background of the text in one color and it matches perfectly with the tables (unlike the highlight tool, which only affects the background of the area the text passes through), and I didn't realise those were two different things. I was painting the tables with the bucket tool thinking it was doing the same as changing the table background color when in reality it was just covering it up with another color. After that I cleaned all the background text color and the changes started showing up, and the macros working. Sorry for all the confusion, everything is working fine now, both the first and second code snippets.

For future readers: Highlight, background color and table background color are THREE different things. Make sure to check which one you changed when making any codes that affect them.
Apache OpenOffice 4.1.7
Windows 10 Pro
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

Found yet another oddity: There is a difference between TABLE, LINE and CELL color. Found this when trying to apply the macro to another document, tables were all scrambled in where their color was applied from and because of that the macro didn't fully work, it did change all the table colors but not their line and cell colors (which I believe take priority over the general table color).

I will see if I can edit the code so it changes all of those into "no background color", that way only the general table color that was specified is applied.
UPDATE: Not having too much luck with this :crazy: ... I never used the Basic programming language before and I am not sure where to look for documentation (found this but doesn't seem to have what I am looking for: https://www.openoffice.org/api/docs/com ... le-ix.html). Would it be too much to ask to add that to the code snippet above by someone who knows better?

I tried using this but it didn't seem to do anything in regards to cell and row colors (same as the code snippet shown in the above replies by Jeje but with a couple lines added in the end):

Code: Select all

sub ChangeTableBackColors3
dim document   as object
dim dispatcher as object
vc = thiscomponent.currentcontroller.viewcursor
if isempty(vc.texttable) = false then
col1 =vc.texttable.backcolor
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:TableDialog", "", 0, array)
col2 =vc.texttable.backcolor
if col1 <> col2 then
tbls = thiscomponent.texttables
for i = 0 to tbls.count -1
if tbls(i).BackColor = col1 then tbls(i).BackColor = col2
tbls(i).IsCellBackgroundTransparent = true
tbls(i).IsRowBackgroundTransparent = true
next
end if
end if
end sub
Apache OpenOffice 4.1.7
Windows 10 Pro
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Change All Instances of one Colour

Post by JeJe »

The following sub will clear the row and cell backgrounds in the current table - where the text cursor is

(put the cursor in each table where you want to clear those and run the macro)

Code: Select all


Sub ClearTableCellAndRowBackGrounds
	vc = thiscomponent.currentcontroller.viewcursor
	if isempty(vc.texttable) = false then
		tbl = vc.texttable


		for i= 0 to tbl.rows.count -1 'clear each row
			tbl.rows(i).backcolor = -1
		next


		for i= 0 to ubound(tbl.cellnames) 'clear each cell
			tbl.getcellbyname(tbl.cellnames(i)).backcolor =-1
		next

	end if
End Sub
OR the following sub will clear the row and cell backgrounds IN ALL THE DOCUMENT'S TABLES

Code: Select all

Sub ClearALLTableCellAndRowBackGrounds
for t= 0 to thiscomponent.texttables.count -1
		tbl = thiscomponent.texttables(t)


		for i= 0 to tbl.rows.count -1 'clear each row
			tbl.rows(i).backcolor = -1
		next


		for i= 0 to ubound(tbl.cellnames) 'clear each cell
			tbl.getcellbyname(tbl.cellnames(i)).backcolor =-1
		next

next

end sub

In both cases the table background color will not be changed
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Minhoca
Posts: 7
Joined: Mon Sep 18, 2023 12:39 am

Re: [Solved] Change All Instances of one Colour

Post by Minhoca »

Thanks, that fixed it.
Apache OpenOffice 4.1.7
Windows 10 Pro
Bill
Volunteer
Posts: 8932
Joined: Sat Nov 24, 2007 6:48 am

Re: [Solved] Change All Instances of one Colour

Post by Bill »

Minhoca wrote: Sun Sep 24, 2023 6:42 pm ...there is a difference between TABLE BACKGROUND COLOR and BACKGROUND (everything?) COLOR. There is a paint bucket tool on the top of the screw that paints the whole background of the text in one color and it matches perfectly with the tables (unlike the highlight tool, which only affects the background of the area the text passes through)...
The paint bucket tool changes the background color of whatever is selected when the tool is used. Click in a paragraph and the tool changes the paragraph background color. Select some text characters and the tool changes the background color of the selected characters. Select one or more cells in a table and the tool changes the background color of the selected cells. Select a table and the tool changes the background color of all cells in the table.
AOO 4.1.14 on Ubuntu MATE 22.04
Post Reply