在 Open/Libre/Neo Office 的文本选择中查找段落的宏

Macro to find paragraphs in text selection in Open/Libre/Neo Office

我正在尝试枚举用户在 (Neo|Libre|Open)Office 中选择的段落。

当我使用下面的代码时,修改版本来自 here

Sub CheckForSelection
    Dim oDoc as Object
    Dim oText

    oDoc = ThisComponent
    oText = oDoc.Text

    if not IsAnythingSelected(oDoc) then
        msgbox("No text selected!")
        Exit Sub
    end if

    oSelections = oDoc.getCurrentSelection() 
    oSel = oSelections.getByIndex(0)    

    ' Info box
    'MsgBox oSel.getString(), 64, "Your Selection"

    oPE = oSel.Text.createEnumeration()
    nPars = 0
    Do While oPE.hasMoreElements()
        oPar = oPE.nextElement()
        REM The returned paragraph will be a paragraph or a text table
        If oPar.supportsService("com.sun.star.text.Paragraph") Then 
            nPars = nPars + 1
        ElseIf oPar.supportsService("com.sun.star.text.TextTable") Then 
            nTables = nTables + 1
        end if
    Loop

    ' Info box
    MsgBox "You selection has " & nPars & " paragraphs.", 64

end Sub

它会找到文档中的所有段落,而不仅仅是选择中的段落。 Google 让我失望了。关于如何在选择中找到个别段落有什么想法吗?

oSel.TextoSel.getText() 的快捷方式,"Returns the text interface in which the text position is contained." https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextRange.html#getText

所以要仅从 Selection 获得 ParagraphEnumeration,您应该使用 oPE = oSel.createEnumeration() 而不是 oPE = oSel.Text.createEnumeration()