[Solved] Add all underlined words to dictionary

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

[Solved] Add all underlined words to dictionary

Post by Tommy »

is it possible to create a macro to batch add to dictionary all underlined words in a Writer document?

actually i have to do it word by word manually with right click menu.

i'dl like to select all the text and then click a button to add all the underlined words to the disctionary with just one click

thanks.
Last edited by Tommy on Sun Jan 27, 2008 10:29 am, edited 2 times in total.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
esperantisto
Volunteer
Posts: 578
Joined: Mon Oct 08, 2007 1:31 am

Re: add all underlined words to dictionary

Post by esperantisto »

I know, this won't be a good answer, but until someone suggest a better one: try to search for a macro ListarPalabrasIncorrectas, I can't remember, where I've seen it. The macro collects all words spelled incorrectly to a separate document. Then, it won't be a big problem either to manually copy/paste the whole lot to a dictionary, or to create a simple macro to add all words. Maybe, even the above macro can be modified accordingly.
AOO 4.2.0 (of 2015) / LO 7.x / Win 7 / openSUSE Linux Leap 15.4 (64-bit)
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: add all underlined words to dictionary

Post by Tommy »

well, is not what i had in mind, but it could be a good alternative method.

i searched over Google that macro and i found it here:

http://www.mail-archive.com/users@es.op ... 01456.html

it seems that the code is this one:

Code: Select all

REM  *****  BASIC  *****

Sub ListarPalabrasIncorrectas

        Dim oDocModel as Variant
        Dim oTextCursor as Variant
        Dim oLinguSvcMgr as Variant
        Dim oSpellChk as Variant
        Dim oListDocFrame as Variant
        Dim oListDocModel as Variant
        Dim sListaPalabras as String
        Dim aProp(0) As New com.sun.star.beans.PropertyValue

        ' Obtener acceso al documento actual
        oDocModel = StarDesktop.CurrentFrame.Controller.getModel()
        If IsNull(oDocModel) Then
                MsgBox("No hay ningún documento activo." + Chr(13) + _
                       "Abra un documento de Writer antes de activar esta 
macro.")
                Exit Sub
        End If

        ' Verificar que este sea un documento de texto
        If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") 
Then
                MsgBox("Este documento no soporta la interfaz 'XTextDocument'." 
+ Chr(13) + _
                       "Utilice esta macro únicamente con documentos de 
Writer.")
                Exit Sub
        End If

        ' Obtener un cursor de texto y posicionarlo al principio del documento
        oTextCursor = oDocModel.Text.createTextCursor()
        oTextCursor.gotoStart(False)

        ' Obtener una referencia al corrector ortográfico o morir en el intento
        oLinguSvcMgr = 
createUnoService("com.sun.star.linguistic2.LinguServiceManager")
        If Not IsNull(oLinguSvcMgr) Then
                oSpellChk = oLinguSvcMgr.getSpellChecker()
        End If
        If IsNull (oSpellChk) Then
                MsgBox("No se pudo acceder a un corrector ortográfico." + 
Chr(13) + _
                       "Verifique las opciones de lingüística de su 
instalación.")
                Exit Sub
        End If
        
        ' Iterar sobre todas las palabras que contiene el documento
        Do
                If oTextCursor.isStartOfWord() Then
                        oTextCursor.gotoEndOfWord(True)
                        ' Verificar si la palabra está bien escrita
                        If Not oSpellChk.isValid(oTextCursor.getString(), 
oTextCursor.getPropertyValue("CharLocale"), aProp()) Then
                                sListaPalabras = sListaPalabras + 
oTextCursor.getString() + Chr(13)
                        End If
                        oTextCursor.collapseToEnd()
                End If
        Loop While oTextCursor.gotoNextWord(False)
        
        If Len(sListaPalabras) = 0 Then
                MsgBox("No hay errores ortográficos en el documento.")
                Exit Sub
        End If

        ' Buscar el frame que contiene la lista (o crearlo)
        oListDocFrame = StarDesktop.findFrame("fListarPalabrasIncorrectas", 
com.sun.star.frame.FrameSearchFlag.ALL)
        If IsNull(oListDocFrame) Then
                oListDocModel = 
StarDesktop.loadComponentFromURL("private:factory/swriter", 
"fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.CREATE, 
aProp())
                oListDocFrame = oListDocModel.CurrentController.getFrame()
        Else
                oListDocModel = oListDocFrame.Controller.getModel()
        End If

        ' Obtener un cursor de texto para este documento
        oTextCursor = oListDocModel.Text.createTextCursor()
        oTextCursor.gotoEnd(False)

        ' Escribir la lista
        oListDocModel.Text.insertString (oTextCursor, sListaPalabras, False)

        ' Activar este frame
        oListDocFrame.activate()

        ' ¡Y ya terminamos!

End Sub
i pasted the code in my edit macro window (Alt+F11 -> Edit)

then i opened a file with some text inside it and i tried to execute the macro.

unfortunately i get a sintax error that points to this line:

"Abra un documento de Writer antes de activar esta
macro."

would you please make a test to see if you have the same crash i have?

unfortunately the macro was written in spanish so i don't understand many words inside it

however it seems that other users had the same problem with it:
http://www.mail-archive.com/users@es.op ... 01466.html
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: add all underlined words to dictionary

Post by Tommy »

nevermind!!!

i managed how to do it.

i tried to edit the macro code, removing the line that made it crash and traslating the Spanish messages in English.

here's the edited version of the macro:

Code: Select all

Sub WrongWordsList 

    Dim oDocModel as Variant 
    Dim oTextCursor as Variant 
    Dim oLinguSvcMgr as Variant 
    Dim oSpellChk as Variant 
    Dim oListDocFrame as Variant 
    Dim oListDocModel as Variant 
    Dim sListaPalabras as String 
    Dim aProp() As New com.sun.star.beans.PropertyValue 

    oDocModel = StarDesktop.CurrentFrame.Controller.getModel() 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document." + Chr(13)) 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface." + Chr(13)) 
        Exit Sub 
    End If 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck." + Chr(13)) 
        Exit Sub 
    End If 
        
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            ' Verificar si la palabra está bien escrita 
            If Not isEmpty (oTextCursor.getPropertyValue("CharLocale")) Then 
                    If Not oSpellChk.isValid(oTextCursor.getString(), oTextCursor.getPropertyValue("CharLocale"), aProp()) Then 
               sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
           End If 
        End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocFrame = StarDesktop.findFrame("fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.ALL) 
    If IsNull(oListDocFrame) Then 
        oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.CREATE, aProp()) 
        oListDocFrame = oListDocModel.CurrentController.getFrame() 
    Else 
        oListDocModel = oListDocFrame.Controller.getModel() 
    End If 

    oTextCursor = oListDocModel.Text.createTextCursor() 
    oTextCursor.gotoEnd(False) 

    oListDocModel.Text.insertString (oTextCursor, sListaPalabras, False) 

    oListDocFrame.activate() 

End Sub
now it works in my PC.
it create a list of all the underlined words on a separate document.
now i can manually autocorrect the wrong mispelled words
but once i got rid of them i remain with correct words which are underlined just becuase they are not yet included in the dictionary.

the next step should be to create a macro that automatically adds those correct words to the dictionary.
does anybody has an idea how i can do it?

by the way, many thanks to esperantisto who gave me the right clue-
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [partially solved] add all underlined words to dictionary

Post by Tommy »

now it's finally solved.

i found the right macro in this topic:
http://www.oooforum.org/forum/viewtopic ... f75287d245

here's the code of the amcro i called BatchAdd2Dic:

Code: Select all

Sub BatchAdd2Dic 

    Dim oDocModel as Variant 
    Dim oTextCursor as Variant 
    Dim oLinguSvcMgr as Variant 
    Dim oSpellChk as Variant 
    Dim oListDocFrame as Variant 
    Dim oListDocModel as Variant 
    Dim sListaPalabras as String 
    Dim aProp() As New com.sun.star.beans.PropertyValue 

    oDocModel = StarDesktop.CurrentFrame.Controller.getModel() 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document." + Chr(13)) 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface." + Chr(13)) 
        Exit Sub 
    End If 
    
' service 
oWBListe = createUnoService ("com.sun.star.linguistic2.DictionaryList") 

' Wörterbuch (WB) 
' Name des Standards: standard.dic 
oWB = oWBListe.getDictionaryByName("standard.dic") 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck." + Chr(13)) 
        Exit Sub 
    End If 
        
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            ' Verificar si la palabra está bien escrita 
            If Not isEmpty (oTextCursor.getPropertyValue("CharLocale")) Then 
                    If Not oSpellChk.isValid(oTextCursor.getString(), oTextCursor.getPropertyValue("CharLocale"), aProp()) Then 
                                        oWB.add( oTextCursor.getString() , FALSE, "" ) 
               sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
           End If 
        End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocFrame = StarDesktop.findFrame("fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.ALL) 
    If IsNull(oListDocFrame) Then 
        oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.CREATE, aProp()) 
        oListDocFrame = oListDocModel.CurrentController.getFrame() 
    Else 
        oListDocModel = oListDocFrame.Controller.getModel() 
    End If 

    oTextCursor = oListDocModel.Text.createTextCursor() 
    oTextCursor.gotoEnd(False) 

    oListDocModel.Text.insertString (oTextCursor, sListaPalabras, False) 

    oListDocFrame.activate() 

End Sub
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Post by Tommy »

if there's anybody interested in creating and auto-installing version with a toolbar button, feel free to use that code. :P
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Post by Tommy »

i used a lot this add2dic auto macro in OOo 2.x
now after upgrading to the 3.0.1 release the macro stopped to work.

when i launch it i got an errore message relative to this code line:

If Not oSpellChk.isValid(oTextCursor.getString(),
oTextCursor.getPropertyValue("CharLocale"), aProp()) Then

what's wrong? it seems that this code is not compatible with OOo 3.0.1.
is there any way to fix the code to make the macro back to work again?

thanks
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Post by Tommy »

i got the solution from the OOo Italian Google Discussion Group (credits to user "martello")

if you change the line:

If Not oSpellChk.isValid(oTextCursor.getString(),
oTextCursor.getPropertyValue("CharLocale"), aProp()) Then

with:

If Not
oSpellChk.isValid(oTextCursor.getString(),oTextCursor.getPropertyValue("CharLocale").Language,
aProp()) Then

the macro work fine even on OOo 3.0.1.
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Post by Tommy »

so here's the OOo 3.0.1 edited version of the WrongWordsList macro.

Code: Select all

Sub WrongWordsList 

    Dim oDocModel as Variant 
    Dim oTextCursor as Variant 
    Dim oLinguSvcMgr as Variant 
    Dim oSpellChk as Variant 
    Dim oListDocFrame as Variant 
    Dim oListDocModel as Variant 
    Dim sListaPalabras as String 
    Dim aProp() As New com.sun.star.beans.PropertyValue 

    oDocModel = StarDesktop.CurrentFrame.Controller.getModel() 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document." + Chr(13)) 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface." + Chr(13)) 
        Exit Sub 
    End If 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck." + Chr(13)) 
        Exit Sub 
    End If 
        
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            ' Verificar si la palabra está bien escrita 
            If Not isEmpty (oTextCursor.getPropertyValue("CharLocale")) Then 
                    If Not oSpellChk.isValid(oTextCursor.getString(),oTextCursor.getPropertyValue("CharLocale").Language, aProp()) Then
               sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
           End If 
        End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocFrame = StarDesktop.findFrame("fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.ALL) 
    If IsNull(oListDocFrame) Then 
        oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "fListarPalabrasIncorrectas", com.sun.star.frame.FrameSearchFlag.CREATE, aProp()) 
        oListDocFrame = oListDocModel.CurrentController.getFrame() 
    Else 
        oListDocModel = oListDocFrame.Controller.getModel() 
    End If 

    oTextCursor = oListDocModel.Text.createTextCursor() 
    oTextCursor.gotoEnd(False) 

    oListDocModel.Text.insertString (oTextCursor, sListaPalabras, False) 

    oListDocFrame.activate() 

End Sub

there's one strange thinh though...

the macro does its job if a text is in italian. you click it and a wrong words list is generated.

if the text is in english the macro creates a list with all the words of the document... both errors and correct words are inside the list...
the macro was supposed to extract only the errors (i.e. words underlined in red...)

i have Windows Vista Home Premium 64bit SP1.
i have both italian and english dictionary extensions installed for all users.

i'm wondering why oit works in italian and doesn't work properly in english...

any idea?
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
User avatar
Tommy
Posts: 251
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Post by Tommy »

the problem was fixed by ms777 on the OOoForum: http://www.oooforum.org/forum/viewtopic ... 878#315878

here's the modified version of the macro that works on OOo 3.0.1

Code: Select all

Sub WrongWordsList 
    oDocModel = ThisComponent 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document.") 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface.") 
        Exit Sub 
    End If 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck.") 
        Exit Sub 
    End If 
        
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            oCharLoc = oTextCursor.getPropertyValue("CharLocale") 
            If Not isEmpty (oCharLoc) Then 
               If Not oSpellChk.com_sun_star_linguistic2_XSpellChecker_isValid(oTextCursor.getString(),oCharLoc, Array()) Then 
                  sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
               End If 
            End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "_default", 0, Array()) 
    oListDocModel.Text.String = sListaPalabras 
    oListDocModel.CurrentController.Frame.activate() 

End Sub
-----
using latest X-LibreOffice release, made portable by winPenPack.com
http://www.winpenpack.com/main/download.php?view.1354
ars
Posts: 5
Joined: Sat May 07, 2016 4:06 pm

Re: [Solved] Add all underlined words to dictionary

Post by ars »

I am a raw beginner trying to use the March 8 2009 version of the WrongWordsList macro on the system configuration in my signature below as of May 2016, and I just received the following error message: "Inadmissible value or data type. [/] Index out of defined range."; and the program statement flagged (from the macro posted above) is:
If Not oSpellChk.com_sun_star_linguistic2_XSpellChecker_isValid(oTextCursor.getString(),oCharLoc, Array()) Then
The vicinity of the flagged statement looks as follows:
Do
If oTextCursor.isStartOfWord() Then
oTextCursor.gotoEndOfWord(True)
oCharLoc = oTextCursor.getPropertyValue("CharLocale")
If Not isEmpty (oCharLoc) Then
If Not oSpellChk.com_sun_star_linguistic2_XSpellChecker_isValid(oTextCursor.getString(),oCharLoc, Array()) Then
sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13)
End If
End If
oTextCursor.collapseToEnd()
End If
Loop While oTextCursor.gotoNextWord(False)
As a raw beginner, I don't know where to begin to debug the macro myself. After all these years, is there any hope anybody knows what is happening and how to update the macro for my system configuration [below]?
Apache OpenOffice 4.0.1 Mac OS X 10.6.8
JoKalliauer
Posts: 1
Joined: Fri Dec 04, 2020 10:53 pm

Re: [Solved] Add all underlined words to dictionary

Post by JoKalliauer »

Tommy wrote:the problem was fixed by ms777 on the OOoForum: http://www.oooforum.org/forum/viewtopic ... 878#315878

here's the modified version of the macro that works on OOo 3.0.1

Code: Select all

Sub WrongWordsList 
    oDocModel = ThisComponent 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document.") 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface.") 
        Exit Sub 
    End If 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck.") 
        Exit Sub 
    End If 
        
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            oCharLoc = oTextCursor.getPropertyValue("CharLocale") 
            If Not isEmpty (oCharLoc) Then 
               If Not oSpellChk.com_sun_star_linguistic2_XSpellChecker_isValid(oTextCursor.getString(),oCharLoc, Array()) Then 
                  sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
               End If 
            End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "_default", 0, Array()) 
    oListDocModel.Text.String = sListaPalabras 
    oListDocModel.CurrentController.Frame.activate() 

End Sub

I get the error "Unsupported datatype" in line 31 (LibreOffice 7.0)
sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13)
Libre Office 7.0 on Fedora 33
mikekaganski
Posts: 13
Joined: Mon Oct 30, 2017 12:39 pm

Re: [Solved] Add all underlined words to dictionary

Post by mikekaganski »

JoKalliauer wrote: Fri Dec 04, 2020 10:58 pm I get the error "Unsupported datatype" in line 31 (LibreOffice 7.0)
sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13)
This is because tdf#124605 was fixed in LibreOffice 6.3. The sListaPalabras variable needs to be initialized to an empty string prior to use in "summation" (concatenation).

Code: Select all

Sub WrongWordsList 
    oDocModel = ThisComponent 
    If IsNull(oDocModel) Then 
        MsgBox("There's no active document.") 
        Exit Sub 
    End If 

    If Not HasUnoInterfaces (oDocModel, "com.sun.star.text.XTextDocument") Then 
        MsgBox("This document doesn't support the 'XTextDocument' interface.") 
        Exit Sub 
    End If 

    oTextCursor = oDocModel.Text.createTextCursor() 
    oTextCursor.gotoStart(False) 

    oLinguSvcMgr = createUnoService("com.sun.star.linguistic2.LinguServiceManager") 
    If Not IsNull(oLinguSvcMgr) Then 
        oSpellChk = oLinguSvcMgr.getSpellChecker() 
    End If 
    If IsNull (oSpellChk) Then 
        MsgBox("It's not possible to access to the spellcheck.") 
        Exit Sub 
    End If 

    sListaPalabras = ""
    Do 
        If oTextCursor.isStartOfWord() Then 
            oTextCursor.gotoEndOfWord(True) 
            oCharLoc = oTextCursor.getPropertyValue("CharLocale") 
            If Not isEmpty (oCharLoc) Then 
               If Not oSpellChk.com_sun_star_linguistic2_XSpellChecker_isValid(oTextCursor.getString(),oCharLoc, Array()) Then 
                  sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13) 
               End If 
            End If 
            oTextCursor.collapseToEnd() 
        End If 
    Loop While oTextCursor.gotoNextWord(False) 
        
    If Len(sListaPalabras) = 0 Then 
        MsgBox("There are no errors in the document.") 
        Exit Sub 
    End If 

    oListDocModel = StarDesktop.loadComponentFromURL("private:factory/swriter", "_default", 0, Array()) 
    oListDocModel.Text.String = sListaPalabras 
    oListDocModel.CurrentController.Frame.activate() 

End Sub
LibreOffice 7.6 on Windows 10
esperantisto
Volunteer
Posts: 578
Joined: Mon Oct 08, 2007 1:31 am

Re: [Solved] Add all underlined words to dictionary

Post by esperantisto »

Mike, thanks a lot for fixing the code! Tested to work in LO 7.4.3.2.
AOO 4.2.0 (of 2015) / LO 7.x / Win 7 / openSUSE Linux Leap 15.4 (64-bit)
Post Reply