libreofficelibreoffice-writer

remove double quotes using a macro


I am using this function and it is working as expected. It removes all punctuation.

  1. The first problem is that it does not remove double quotes "
  2. The second problem is that I need to select text that needs to be processed. I will prefer that current file (all text) corrected by default.
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        , "'", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , ",", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\(", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\)", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\*", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\-", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\;", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\?", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\[", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\]", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\–", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\—", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\‘", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\“", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\”", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\.", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\:", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\'", " ", "g"))
h = fa.callFunction("REGEX", Array(h        , "\uFEFF", " ", "g"))
rg.String = h
Next k
End Sub

Solution

    1. This works for me:
    h = fa.callFunction("REGEX", Array(h, """", " ", "g"))
    
    1. I know next to nothing about VBA and LibreOffice so probably this is not an optimal solution but it works somehow with no selection:
    Sub removePunc()
    
        fa = createUnoService("com.sun.star.sheet.FunctionAccess")
        rg = ThisComponent.Text
    
        h = fa.callFunction("REGEX", Array(rg.String, "!", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "'", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , ",", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\(", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\)", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\*", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\-", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\;", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\?", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\[", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\]", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\–", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\—", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\‘", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\“", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\”", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , "\.", " ", "g"))
        h = fa.callFunction("REGEX", Array(h        , """", " ", "g"))
        
        ThisComponent.Text.String = h
    
    End Sub
    

    Actually all your changes can be done in one step:

    Find for: "[!',\(\)\*\-;\?\[\]\–\—‘“”\.\""]"

    Replace with: (space)

    Nevertheless if you want to use a list of changes here is the more optimal implementation:

    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
    

    Based on the official examples: https://api.libreoffice.org/examples/basic/text/modifying_text_automatically/

    It does replacement in one step as well, but you can add to the arrays additional elements if you want, this way:

    to_search()  = Array("no", "never", "no way!")
    to_replace() = Array("yes", "always", "why not?")