问题描述
Sub removePunc()
REM the text ranges to work on must be seleczed in advance.
REM This will be done mostly by a F&R action with an appropriate
REM search strung and 'Find All'.
REM the this macro can be run.
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
rgs = ThisComponent.CurrentSelection
n = rgs.Count -1
For k = 0 To n
rg = rgs(k)
h = fa.callFunction("REGEX",Array(rg.String,"!"," ","g"))
h = fa.callFunction("REGEX",Array(h,"'",","\(","\)","\*","\-","\;","\?","\[","\]","\–","\—","\‘","\“","\”","\.","\:","\'","\uFEFF","g"))
rg.String = h
Next k
End Sub
解决方法
- 这对我有用:
h = fa.callFunction("REGEX",Array(h,""""," ","g"))
- 我对 VBA 和 LibreOffice 几乎一无所知,所以这可能不是最佳解决方案,但它可以在没有选择的情况下以某种方式工作:
Sub removePunc()
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
rg = ThisComponent.Text
h = fa.callFunction("REGEX",Array(rg.String,"!","g"))
h = fa.callFunction("REGEX","'",","\(","\)","\*","\-","\;","\?","\[","\]","\–","\—","\‘","\“","\”","\.","g"))
ThisComponent.Text.String = h
End Sub
实际上,您可以一步完成所有更改:
查找:"[!',\(\)\*\-;\?\[\]\–\—‘“”\.\""]"
替换为:
(空格)
尽管如此,如果您想使用更改列表,这里是更优化的实现:
Sub Replace
Dim to_search() As String
Dim to_replace() As String
Dim n As Long
Dim oDocument As Object
Dim oReplace As Object
to_search() = Array("[!',\(\)\*\-;\?\[\]\–\—‘“”\.\""]")
to_replace() = Array(" ")
oDocument = ThisComponent
oReplace = oDocument.createReplaceDescriptor
oReplace.SearchRegularExpression = True
For n = lbound(to_search()) To ubound(to_search())
oReplace.SearchString = to_search(n)
oReplace.ReplaceString = to_replace(n)
oDocument.replaceAll(oReplace)
Next n
End Sub
基于官方示例:https://api.libreoffice.org/examples/basic/text/modifying_text_automatically/
它也可以一步完成替换,但是如果需要,您可以通过这种方式向数组添加其他元素:
to_search() = Array("no","never","no way!")
to_replace() = Array("yes","always","why not?")
,
感谢用户@Yuri khristich 这按预期工作:
Sub removePunc()
fa = createUnoService("com.sun.star.sheet.FunctionAccess")
rg = ThisComponent.Text
h = fa.callFunction("REGEX","[!',\:\(\)\*\-;\?\[\]\–\—‘“”\.\""]","\uFEFF","g"))
ThisComponent.Text.String = h
ListMisSpelledWords3
End Sub