1

I've got a LibreOffice Writer macro that finds the next Heading paragraph and converts it to Title case. Currently I have to invoke it repeatedly until the end of file is reached. I'm trying to set up a loop that will do everything, but stop at the EOF. But the loop is not working.

Any help would be appreciated. Here's what I have.

sub Convert_Headings_to_Title_Case

rem define variables
    dim document   as Object
    dim dispatcher as Object
    Dim Proceed As boolean

rem get access to the document
    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem loop not working
Do 
' Call other macro to find next Heading:
    Heading_findNext

    dispatcher.executeDispatch(document, ".uno:EndOfLineSel", "", 0, Array())

    dispatcher.executeDispatch(document, ".uno:ChangeCaseToTitleCase", "", 0, Array())

Loop While Proceed

end sub

The macro being called to find the headings is:

sub Heading_findNext
'moves text cursor, but not view cursor, to heading
Dim oStyle, oCurs, oDoc, oVC, Proceed
oDoc = ThisComponent.Text
oVC = ThisComponent.CurrentController.getViewCursor
oCurs = ThisComponent.Text.createTextCursorByRange(oVC)

Do
    Proceed = oCurs.gotoNextParagraph(false)
    oStyle = Mid(oCurs.ParaStyleName, 1, 2)
    Select Case oStyle
        Case "_H", "He"
        oVC = ThisComponent.CurrentController.getviewcursor()
        oVC.gotoRange(oCurs, False)
        Exit Do
    End Select
Loop While Proceed <> false
end sub
Paul B.
  • 69

1 Answers1

1

One issue perhaps is that Proceed from the loop in Convert_Headings_to_Title_Case never gets changed. Perhaps you intended to write Heading_findNext as a Function rather than a Sub, and return a boolean value like Proceed = Heading_findNext().

Also, be sure to start the view cursor at the beginning of the document.

Here is correct working code.

Sub Convert_Headings_to_Title_Case
    Dim oDoc, oFrame, dispatcher As Object
    Dim oVC, oCurs As Object
    Dim sStyleNamePart As String
    oDoc = ThisComponent
    oFrame = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") 
    oVC = oDoc.CurrentController.getViewCursor()
    oVC.gotoStart(False)
    oCurs = oVC.getText().createTextCursorByRange(oVC)
    While oCurs.gotoNextParagraph(False)
        sStyleNamePart = Mid(oCurs.ParaStyleName, 1, 2)
        If sStyleNamePart = "_H" Or sStyleNamePart = "He" Then
            oVC.gotoRange(oCurs, False)
            dispatcher.executeDispatch(oFrame, ".uno:EndOfLineSel", "", 0, Array())
            dispatcher.executeDispatch(_
                oFrame, ".uno:ChangeCaseToTitleCase", "", 0, Array())
        End If
    Wend
End Sub
Jim K
  • 4,439