How to access Paragraph Properties ParaBackColor

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
relativitis
Posts: 2
Joined: Sat Oct 31, 2020 3:58 pm

How to access Paragraph Properties ParaBackColor

Post by relativitis »

Hi People,

i am struggling with a problem, I have many documents created by myself and co-editors they contain highlights of different color.
They are saved in .docx format I know it was an mistake I should have only transferred it to this format in the last step but my clients requested it llike this and I made the mistake to follow them, in my own workflow...for no reason. They contain different colored higlightings and I need to unify some of the colors to only specific ones.

I first attempted it with the find and replace but apparently it is buggy because of the .docx format or something and it doesnt work. So I thought doing it with a macro. I am really a beginner programmer with only very limited knowledge of python. But together with the macro recorder and the macro editor I played around a little to see what is happening.

I wanted to avoid the find and replace function because of the bug cause I thought the find and replace in the gui also only uses this function so my idea was:

1. Create an array containing the paragraphs. ( Somehow it was easier to just use the enumerate function but it works.)
2. Run a for-loop on the array.
3. Checking if the element is a paragraph.
4. Checking if the highlight color, I want to change is present, with an nested If statement.
5. If it is exchange it with the one specified.
6. Checking another color with If-Statement.
7. Else continue.

Thats it.

Code: Select all

Sub Special

dim oEnum 
dim oPar
dim bumred
dim bumgre
oEnum = ThisComponent.Text.createEnumeration()
oPar = oEnum.nextElement()
REM print oEnum

Do While oEnum.hasMoreElements()

	bumred = oPar.getPropertyValue("CharBackColor")
REM 	print bumred

	If oPar.supportsservice("com.sun.star.text.Paragraph") Then
	
		If bumred Is 9701130 Then
		
			oPar.setPropertyValue("ParaBackColor", 16711680)
			
		End If
	End If


	oPar = oEnum.nextElement()
	
REM	print "nicht rot"


Loop

REM print "don"

End Sub
So that is what I have but the If Statement never runs. And I am not sure about if, if it would run would the ParaBackColor really change.

Thanks people
OpenOffice 2.4 on Ubuntu 9.04
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to access Paragraph Properties ParaBackColor

Post by FJCC »

Are you looking for entire paragraphs that are highlighted or just part of a paragraph? If it is part of a paragraph, you need another While loop that works with each portion of a paragraph. Something like

Code: Select all

oPortionEnum = oPara.createEnumeration
While oPortionEnum.hasMoreElements
  oPor = oPortionEnum.nextElement
  If oPor.CharBackColor = 9701130 Then
    oPor.CharBackColor = 16711680
 End If
WEnd
I typed that code without testing it, so expect some mistakes!
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.
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to access Paragraph Properties ParaBackColor

Post by JeJe »

You can do multiple changes at once if you list them in a select case statement

Code: Select all


Sub wholeparacolors

	dim oEnum
	dim oPar
	dim bumred
	dim bumgre
	oEnum = ThisComponent.Text.createEnumeration()

	Do While oEnum.hasMoreElements()

		oPar = oEnum.nextElement()
		If oPar.supportsservice("com.sun.star.text.Paragraph") Then
			select case opar.CharBackColor
			case 16711680
				oPar.CharBackColor = 998877
			case  9701130
				oPar.CharBackColor = 16711680
			end select
		End If
	loop
End Sub

Sub Partparacolors

	dim oEnum
	dim oPar
	dim bumred
	dim bumgre
	oEnum = ThisComponent.Text.createEnumeration()

	Do While oEnum.hasMoreElements()

		oPar = oEnum.nextElement()
		If oPar.supportsservice("com.sun.star.text.Paragraph") Then
			en = opar.createenumeration
			do while en.hasmoreelements
				p= en.nextelement
				select case p.CharBackColor
				case  9701130
					P.CharBackColor = 16711680
				end select
		loop
	End If
	loop
End Sub

Edit: that's in basic of course.
If the backcolors have been applied to the paragraph or a character style rather than applied directly you'd be better modifying the style though.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
relativitis
Posts: 2
Joined: Sat Oct 31, 2020 3:58 pm

Re: How to access Paragraph Properties ParaBackColor

Post by relativitis »

Hi thanks for the tips. I´ll try now to get it working. I didnt really know about the styles so I just applied everything directly. And as I know the option to define a style later isnt possible. For example like this every Paragraph with this kind of Background is this style. And now just switch the style color. But without messing with my formatting(Not adding a Paragraph or extra line), because the documents I work on are just endless lists of names.

No it is really the whole paragraph that is colored and if the color is present somewhere the whole ParaBackColor should be changed. That is also why I used the CharBackColor because it didnt really matter to me if it just checks the first(at least I assumed it would check this one), letter or the whole paragraph and at first when using the ParaBackColor I couldnt get it running.

Thanks alot I´ll be back!
OpenOffice 2.4 on Ubuntu 9.04
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: How to access Paragraph Properties ParaBackColor

Post by Zizi64 »

And as I know the option to define a style later isnt possible.
You can create and adjust and apply styles every time when you edit a document. But when you has some overwrited style property by directly applied values, then you need delete (reset) the direct formatting properties by the macro code or manually for the style property values to take effect.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to access Paragraph Properties ParaBackColor

Post by Villeroy »

An office suite is made for non-programmers. The concept of styles wrapped in templates obsoletes 95% of Writer macros seen on this forum. Authors and office workers who never write a single line of program code can use this office suite efficiently and after some time of custumization effortlessly. Generating documents by code is the most inefficient way of customization, even if you are very familiar with this monster API. A shortcut or a one-line macro ThisComponent.CurrentSelection.ParagraphStyle = "Subtitle_Quoting" may change hundreds of attributes in one go. And the best thing is: No matter how often you applied some style, you can easily change that style and the change applies instantly to all regions where the style has been applied. Unprofessional users (and macro coders) would search for all occurrances of a certain attribute that needs to be changed. And if you organize your styles in registered templates, you can even apply style changes to documents that have been created already.
OpenOffice Writer can bind styles to keyboard shortcuts. LibreOffice Writer can do the same trick with shortcuts, toolbar buttons, menue entries and context menues.
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
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to access Paragraph Properties ParaBackColor

Post by JeJe »

You can change colored paragraphs to a named paragraph style with .parastylename

If you define your style and set the style backcolor and then want another backcolor instead... its easy just to change it by altering the style.

Code: Select all

Sub wholeparacolors

   dim oEnum
   dim oPar
   dim bumred
   dim bumgre
   oEnum = ThisComponent.Text.createEnumeration()

   Do While oEnum.hasMoreElements()

      oPar = oEnum.nextElement()
      If oPar.supportsservice("com.sun.star.text.Paragraph") Then
         select case opar.CharBackColor
         case 16711680
            oPar.parastylename = "nameofyourstyle" 'which you already defined and set to a particular backcolor
         end select
      End If
   loop
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply