libreofficelibreoffice-writer

Can you Print the wavy lines generated by Spell check in writer?


As per google group, this macro can be used to print mis-spelled words in MS office.

https://groups.google.com/g/microsoft.public.word.spelling.grammar/c/OiFYPkLAbeU

Is there similar option in libre-office writer?


Solution

  • The following Subroutine replicates what the code in the Google group does. It is more verbose than the MS version but that is to be expected with LibreOffice / OpenOffice. It only does the spellchecker lines and not the green grammar checker ones, which is also the case with the MS version in the Google group.

    Sub UnderlineMisspelledWords
    
        ' From OOME Listing 315 Page 336
        GlobalScope.BasicLibraries.loadLibrary( "Tools" )
        Dim sLocale As String
        sLocale = GetRegistryKeyContent("org.openoffice.Setup/L10N", FALSE).getByName("ooLocale")
    
        ' ooLocale appears to return a string that consists of the language and country
        ' seperated by a dash, e.g. en-GB
        Dim nDash As Integer
        nDash = InStr(sLocale, "-")
    
        Dim aLocale As New com.sun.star.lang.Locale
        aLocale.Language = Left(sLocale, nDash - 1)
        aLocale.Country = Right(sLocale, Len(sLocale) -nDash )
    
        Dim oSpeller As Variant
        oSpeller = createUnoService("com.sun.star.linguistic2.SpellChecker")
    
        Dim emptyArgs() as new com.sun.star.beans.PropertyValue
    
        Dim oCursor As Object
        oCursor = ThisComponent.getText.createTextCursor()
        oCursor.gotoStart(False)
        oCursor.collapseToStart()
    
        Dim s as String, bTest As Boolean
        Do 
            oCursor.gotoEndOfWord(True)
            s = oCursor.getString()
            bTest = oSpeller.isValid(s, aLocale, emptyArgs())
    
            If Not bTest Then    
                With oCursor
                    .CharUnderlineHasColor = True
                    .CharUnderlineColor = RGB(255, 0,0)
                    .CharUnderline = com.sun.star.awt.FontUnderline.WAVE
                    ' Possible alternatives include SMALLWAVE, DOUBLEWAVE and BOLDWAVE
                End With
            End If    
        Loop While oCursor.gotoNextWord(False)
    
    End Sub    
    

    This will change the actual formatting of the font to have a red wavy underline, which will print out like any other formatting. If any of the misspelled words in the document already have some sort of underlining then that will be lost.

    You will probably want to remove the underlining after you have printed it. The following Sub removes underlining only where its style exactly matches that of the line added by the first routine.

    Sub RemoveUnderlining
    
        Dim oCursor As Object
        oCursor = ThisComponent.getText.createTextCursor()
        oCursor.gotoStart(False)
        oCursor.collapseToStart()
    
        Dim s as String, bTest As Boolean
        Do 
        
            oCursor.gotoEndOfWord(True) 
            
            Dim bTest1 As Boolean        
            bTest1 = False
            If oCursor.CharUnderlineHasColor = True Then
                bTest1 = True
            End If
            
            Dim bTest2 As Boolean  
            bTest2 = False
            If oCursor.CharUnderlineColor = RGB(255, 0,0) Then
                bTest2 = True
            End If
            
            Dim bTest3 As Boolean  
            bTest3 = False
            If oCursor.CharUnderline = com.sun.star.awt.FontUnderline.WAVE Then
                bTest3 = True
            End If
            
            If bTest1 And bTest2 And bTest3 Then
                With oCursor
                    .CharUnderlineHasColor = False
                    .CharUnderline = com.sun.star.awt.FontUnderline.NONE
                End With
            End If
        Loop While oCursor.gotoNextWord(False)
    
    End Sub
    

    This will not restore any original underlining that was replaced by red wavy ones. Other ways of removing the wavy lines that would restore these are:

    1. Pressing undo (Ctrl Z) but you will need to do that once for every word in your document, which could be a bit of a pain.

    2. Running the subroutine UnderlineMisspelledWords on a temporary copy of the document and then discarding it after printing.

    I hope this is what you were looking for.