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

时间:2015-03-29 20:09:29

标签: openoffice.org libreoffice basic

我试图枚举用户在(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

它会在文档中找到所有段落,而不仅仅是在选择中。谷歌让我失望了。如何在选择中找到单个段落?

1 个答案:

答案 0 :(得分:0)

oSel.TextoSel.getText()的快捷方式,其中“返回文本位置 中的文本界面包含。” https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextRange.html#getText

因此,要仅从ParagraphEnumeration获取Selection,您应该使用oPE = oSel.createEnumeration()代替oPE = oSel.Text.createEnumeration()