【问题标题】:Macro to find paragraphs in text selection in Open/Libre/Neo Office在 Open/Libre/Neo Office 中查找文本选择中的段落的宏
【发布时间】:2015-03-29 20:09:28
【问题描述】:

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

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

【问题讨论】:

    标签: openoffice.org libreoffice basic


    【解决方案1】:

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

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

    【讨论】:

    • 我会接受这一点,因为它确实提供了正确数量的所选行/段落,尽管即使选择了部分段落,它也会返回完整段落。就我的目的而言,这已经足够好了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多