我试图枚举用户在(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
它会在文档中找到所有段落,而不仅仅是在选择中。谷歌让我失望了。如何在选择中找到单个段落?
答案 0 :(得分:0)
oSel.Text
是oSel.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()
。