LibreOffice:在文件末尾停止宏循环

LibreOffice:在文件末尾停止宏循环

我有一个 LibreOffice Writer 宏,它可以找到下一个标题段落并将其转换为标题大小写。目前我必须反复调用它,直到到达文件末尾。我试图设置一个循环,它可以完成所有操作,但在 EOF 处停止。但循环不起作用。

任何帮助都将不胜感激。以下是我所拥有的。

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

调用来查找标题的宏是:

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

答案1

一个问题可能是Proceed循环中的值Convert_Headings_to_Title_Case永远不会改变。也许您打算将其编写Heading_findNext为函数而不是 Sub,并返回布尔值,如Proceed = Heading_findNext()

另外,请确保将视图光标置于文档的开头。

这是正确的工作代码。

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

相关内容