简体   繁体   English

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

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

I am trying to enumerate the paragraphs selected by the user in (Neo|Libre|Open)Office. 我正在尝试枚举用户在(Neo | Libre | Open)Office中选择的段落。

When I use the code below, modified version from 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

it finds ALL the paragraphs in the document, not just in the selection. 它可以找到文档中的所有段落,而不仅是所选内容。 Google has failed me. Google让我失败了。 Any thoughts on how to find individual paragraphs in the selection? 关于如何在选择中查找单个段落有什么想法?

The oSel.Text is a shortcut for oSel.getText() which "Returns the text interface in which the text position is contained ." oSel.Text为捷径oSel.getText()其中“返回其中文本位置被包含在文本界面”。 https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextRange.html#getText https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextRange.html#getText

So to get a ParagraphEnumeration only from the Selection , you should use oPE = oSel.createEnumeration() instead of oPE = oSel.Text.createEnumeration() . 因此,仅从Selection获取ParagraphEnumeration ,应使用oPE = oSel.createEnumeration()而不是oPE = oSel.Text.createEnumeration()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM