Make a macro loop until the end of the file

Please, I’m trying to create a macro that changes the style of each second paragraph. In other words, I want to make the paragraphs alternate styles. But I want it to happen only in some parts of the texts. Those parts have a different font size, so I managed to record a macro that does that. What I need now is to make the macro loop until the end of the file. I guess I need to put “Do…Loop” and “Eof Function” in my recorded macro, but I don’t know how to do that. Here is my recorded macro:

sub alternate
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(21) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.SearchFiltered"
args1(4).Value = false
args1(5).Name = "SearchItem.Backward"
args1(5).Value = false
args1(6).Name = "SearchItem.Pattern"
args1(6).Value = false
args1(7).Name = "SearchItem.Content"
args1(7).Value = false
args1(8).Name = "SearchItem.AsianOptions"
args1(8).Value = false
args1(9).Name = "SearchItem.AlgorithmType"
args1(9).Value = 0
args1(10).Name = "SearchItem.SearchFlags"
args1(10).Value = 65536
args1(11).Name = "SearchItem.SearchString"
args1(11).Value = ""
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = ""
args1(13).Name = "SearchItem.Locale"
args1(13).Value = 255
args1(14).Name = "SearchItem.ChangedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.DeletedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.InsertedChars"
args1(16).Value = 2
args1(17).Name = "SearchItem.TransliterateFlags"
args1(17).Value = 1073743104
args1(18).Name = "SearchItem.Command"
args1(18).Value = 0
args1(19).Name = "SearchItem.SearchFormatted"
args1(19).Value = false
args1(20).Name = "SearchItem.AlgorithmType2"
args1(20).Value = 1
args1(21).Name = "Quiet"
args1(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

rem ----------------------------------------------------------------------
dim args2(21) as new com.sun.star.beans.PropertyValue
args2(0).Name = "SearchItem.StyleFamily"
args2(0).Value = 2
args2(1).Name = "SearchItem.CellType"
args2(1).Value = 0
args2(2).Name = "SearchItem.RowDirection"
args2(2).Value = true
args2(3).Name = "SearchItem.AllTables"
args2(3).Value = false
args2(4).Name = "SearchItem.SearchFiltered"
args2(4).Value = false
args2(5).Name = "SearchItem.Backward"
args2(5).Value = false
args2(6).Name = "SearchItem.Pattern"
args2(6).Value = false
args2(7).Name = "SearchItem.Content"
args2(7).Value = false
args2(8).Name = "SearchItem.AsianOptions"
args2(8).Value = false
args2(9).Name = "SearchItem.AlgorithmType"
args2(9).Value = 0
args2(10).Name = "SearchItem.SearchFlags"
args2(10).Value = 65536
args2(11).Name = "SearchItem.SearchString"
args2(11).Value = ""
args2(12).Name = "SearchItem.ReplaceString"
args2(12).Value = ""
args2(13).Name = "SearchItem.Locale"
args2(13).Value = 255
args2(14).Name = "SearchItem.ChangedChars"
args2(14).Value = 2
args2(15).Name = "SearchItem.DeletedChars"
args2(15).Value = 2
args2(16).Name = "SearchItem.InsertedChars"
args2(16).Value = 2
args2(17).Name = "SearchItem.TransliterateFlags"
args2(17).Value = 1073743104
args2(18).Name = "SearchItem.Command"
args2(18).Value = 0
args2(19).Name = "SearchItem.SearchFormatted"
args2(19).Value = false
args2(20).Name = "SearchItem.AlgorithmType2"
args2(20).Value = 1
args2(21).Name = "Quiet"
args2(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(21) as new com.sun.star.beans.PropertyValue
args3(0).Name = "SearchItem.StyleFamily"
args3(0).Value = 2
args3(1).Name = "SearchItem.CellType"
args3(1).Value = 0
args3(2).Name = "SearchItem.RowDirection"
args3(2).Value = true
args3(3).Name = "SearchItem.AllTables"
args3(3).Value = false
args3(4).Name = "SearchItem.SearchFiltered"
args3(4).Value = false
args3(5).Name = "SearchItem.Backward"
args3(5).Value = false
args3(6).Name = "SearchItem.Pattern"
args3(6).Value = false
args3(7).Name = "SearchItem.Content"
args3(7).Value = false
args3(8).Name = "SearchItem.AsianOptions"
args3(8).Value = false
args3(9).Name = "SearchItem.AlgorithmType"
args3(9).Value = 0
args3(10).Name = "SearchItem.SearchFlags"
args3(10).Value = 65536
args3(11).Name = "SearchItem.SearchString"
args3(11).Value = ""
args3(12).Name = "SearchItem.ReplaceString"
args3(12).Value = ""
args3(13).Name = "SearchItem.Locale"
args3(13).Value = 255
args3(14).Name = "SearchItem.ChangedChars"
args3(14).Value = 2
args3(15).Name = "SearchItem.DeletedChars"
args3(15).Value = 2
args3(16).Name = "SearchItem.InsertedChars"
args3(16).Value = 2
args3(17).Name = "SearchItem.TransliterateFlags"
args3(17).Value = 1073743104
args3(18).Name = "SearchItem.Command"
args3(18).Value = 2
args3(19).Name = "SearchItem.SearchFormatted"
args3(19).Value = false
args3(20).Name = "SearchItem.AlgorithmType2"
args3(20).Value = 1
args3(21).Name = "Quiet"
args3(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args3())

rem ----------------------------------------------------------------------
dim args4(21) as new com.sun.star.beans.PropertyValue
args4(0).Name = "SearchItem.StyleFamily"
args4(0).Value = 2
args4(1).Name = "SearchItem.CellType"
args4(1).Value = 0
args4(2).Name = "SearchItem.RowDirection"
args4(2).Value = true
args4(3).Name = "SearchItem.AllTables"
args4(3).Value = false
args4(4).Name = "SearchItem.SearchFiltered"
args4(4).Value = false
args4(5).Name = "SearchItem.Backward"
args4(5).Value = false
args4(6).Name = "SearchItem.Pattern"
args4(6).Value = false
args4(7).Name = "SearchItem.Content"
args4(7).Value = false
args4(8).Name = "SearchItem.AsianOptions"
args4(8).Value = false
args4(9).Name = "SearchItem.AlgorithmType"
args4(9).Value = 0
args4(10).Name = "SearchItem.SearchFlags"
args4(10).Value = 65536
args4(11).Name = "SearchItem.SearchString"
args4(11).Value = ""
args4(12).Name = "SearchItem.ReplaceString"
args4(12).Value = ""
args4(13).Name = "SearchItem.Locale"
args4(13).Value = 255
args4(14).Name = "SearchItem.ChangedChars"
args4(14).Value = 2
args4(15).Name = "SearchItem.DeletedChars"
args4(15).Value = 2
args4(16).Name = "SearchItem.InsertedChars"
args4(16).Value = 2
args4(17).Name = "SearchItem.TransliterateFlags"
args4(17).Value = 1073743104
args4(18).Name = "SearchItem.Command"
args4(18).Value = 0
args4(19).Name = "SearchItem.SearchFormatted"
args4(19).Value = false
args4(20).Name = "SearchItem.AlgorithmType2"
args4(20).Value = 1
args4(21).Name = "Quiet"
args4(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

rem ----------------------------------------------------------------------
dim args5(21) as new com.sun.star.beans.PropertyValue
args5(0).Name = "SearchItem.StyleFamily"
args5(0).Value = 2
args5(1).Name = "SearchItem.CellType"
args5(1).Value = 0
args5(2).Name = "SearchItem.RowDirection"
args5(2).Value = true
args5(3).Name = "SearchItem.AllTables"
args5(3).Value = false
args5(4).Name = "SearchItem.SearchFiltered"
args5(4).Value = false
args5(5).Name = "SearchItem.Backward"
args5(5).Value = false
args5(6).Name = "SearchItem.Pattern"
args5(6).Value = false
args5(7).Name = "SearchItem.Content"
args5(7).Value = false
args5(8).Name = "SearchItem.AsianOptions"
args5(8).Value = false
args5(9).Name = "SearchItem.AlgorithmType"
args5(9).Value = 0
args5(10).Name = "SearchItem.SearchFlags"
args5(10).Value = 65536
args5(11).Name = "SearchItem.SearchString"
args5(11).Value = ""
args5(12).Name = "SearchItem.ReplaceString"
args5(12).Value = ""
args5(13).Name = "SearchItem.Locale"
args5(13).Value = 255
args5(14).Name = "SearchItem.ChangedChars"
args5(14).Value = 2
args5(15).Name = "SearchItem.DeletedChars"
args5(15).Value = 2
args5(16).Name = "SearchItem.InsertedChars"
args5(16).Value = 2
args5(17).Name = "SearchItem.TransliterateFlags"
args5(17).Value = 1073743104
args5(18).Name = "SearchItem.Command"
args5(18).Value = 0
args5(19).Name = "SearchItem.SearchFormatted"
args5(19).Value = false
args5(20).Name = "SearchItem.AlgorithmType2"
args5(20).Value = 1
args5(21).Name = "Quiet"
args5(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args5())

rem ----------------------------------------------------------------------
dim args6(21) as new com.sun.star.beans.PropertyValue
args6(0).Name = "SearchItem.StyleFamily"
args6(0).Value = 2
args6(1).Name = "SearchItem.CellType"
args6(1).Value = 0
args6(2).Name = "SearchItem.RowDirection"
args6(2).Value = true
args6(3).Name = "SearchItem.AllTables"
args6(3).Value = false
args6(4).Name = "SearchItem.SearchFiltered"
args6(4).Value = false
args6(5).Name = "SearchItem.Backward"
args6(5).Value = false
args6(6).Name = "SearchItem.Pattern"
args6(6).Value = false
args6(7).Name = "SearchItem.Content"
args6(7).Value = false
args6(8).Name = "SearchItem.AsianOptions"
args6(8).Value = false
args6(9).Name = "SearchItem.AlgorithmType"
args6(9).Value = 0
args6(10).Name = "SearchItem.SearchFlags"
args6(10).Value = 65536
args6(11).Name = "SearchItem.SearchString"
args6(11).Value = ""
args6(12).Name = "SearchItem.ReplaceString"
args6(12).Value = ""
args6(13).Name = "SearchItem.Locale"
args6(13).Value = 255
args6(14).Name = "SearchItem.ChangedChars"
args6(14).Value = 2
args6(15).Name = "SearchItem.DeletedChars"
args6(15).Value = 2
args6(16).Name = "SearchItem.InsertedChars"
args6(16).Value = 2
args6(17).Name = "SearchItem.TransliterateFlags"
args6(17).Value = 1073743104
args6(18).Name = "SearchItem.Command"
args6(18).Value = 2
args6(19).Name = "SearchItem.SearchFormatted"
args6(19).Value = false
args6(20).Name = "SearchItem.AlgorithmType2"
args6(20).Value = 1
args6(21).Name = "Quiet"
args6(21).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args6())


end sub

Can anyone help, please?
Thanks,

The macro recorder has a limited capability. You must WRITE your macros when you want tho work with the macros efficiently. The simplified “formula” of the macros in the Apache OpenOffice and LibreOffice is:
“A supported programming language + the API functions.”

You need study the very simple StarBasic (as the simpliest and the most obvious supported programming language - note: there are many others) and the very difficult API of the AOO/LO. (There are thusands of the API functions and procedures in the AOO/LO.)

Thanks for your explanation.

And a StarBasic+API code :

    Sub MyStyles

    Dim oDoc as Object
    Dim oText as object
    Dim oPar as object
    Dim bAlternate as boolean
    

    oDoc = ThisComponent
    oText = oDoc.getText()
	bAlternate = false

    oParEnum = oText.createEnumeration()
	Do While oParEnum.hasMoreElements()
		oPar = oParEnum.nextElement()
		REM This avoids the tables. Add an else statement if you want to process the tables.
		If oPar.supportsService("com.sun.star.text.Paragraph") Then
			If bAlternate then
				oPar.ParaStyleName = "MyStyle1"
			else
				oPar.ParaStyleName = "MyStyle2"
			end if
		end if
		bAlternate = not bAlternate
	loop
end Sub

(a modified Andrew Pitonyak’s sample code)

Thanks for your suggestions. But, as I said in my question, “I want it to happen only in some parts of the texts. Those parts have a different font size” (let’s say the font size 12). My recorded loop does that. Now I just need to know how to insert a “repeat until the end of the file” command.

Is trivial, with Python…

def main():
    style1 = 'MyStyle1'
    style2 = 'MyStyle2'

    doc = XSCRIPTCONTEXT.getDocument()
    text = doc.Text

    for i, p in enumerate(text):
        if i % 2:
            p.ParaStyleName = style1
        else:
            p.ParaStyleName = style2
    return

MyStyle1 and 2 must exist

image description

Thanks for your suggestions. But, as I said in my question, “I want it to happen only in some parts of the texts. Those parts have a different font size” (let’s say the font size 12). My recorded loop does that. Now I just need to know how to insert a “repeat until the end of the file” command.

add an “exactly” doc with “exactly” you want…