Page 1 of 1
[Solved] Add all underlined words to dictionary
Posted: Fri Dec 28, 2007 4:13 pm
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.
Re: add all underlined words to dictionary
Posted: Sun Dec 30, 2007 10:45 am
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.
Re: add all underlined words to dictionary
Posted: Sun Dec 30, 2007 11:38 am
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
Re: add all underlined words to dictionary
Posted: Sun Dec 30, 2007 12:05 pm
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-
Re: [partially solved] add all underlined words to dictionary
Posted: Sun Jan 27, 2008 10:29 am
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
Re: [Solved] Add all underlined words to dictionary
Posted: Mon Jan 28, 2008 11:55 am
by Tommy
if there's anybody interested in creating and auto-installing version with a toolbar button, feel free to use that code.
Re: [Solved] Add all underlined words to dictionary
Posted: Sat Feb 07, 2009 12:02 pm
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
Re: [Solved] Add all underlined words to dictionary
Posted: Sun Feb 08, 2009 4:17 pm
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.
Re: [Solved] Add all underlined words to dictionary
Posted: Sun Feb 15, 2009 12:15 pm
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?
Re: [Solved] Add all underlined words to dictionary
Posted: Mon Mar 09, 2009 12:00 am
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
Re: [Solved] Add all underlined words to dictionary
Posted: Sat May 14, 2016 3:18 am
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]?
Re: [Solved] Add all underlined words to dictionary
Posted: Fri Dec 04, 2020 10:58 pm
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)
Re: [Solved] Add all underlined words to dictionary
Posted: Mon Dec 26, 2022 1:27 pm
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
Re: [Solved] Add all underlined words to dictionary
Posted: Mon Dec 26, 2022 4:57 pm
by esperantisto
Mike, thanks a lot for fixing the code! Tested to work in LO 7.4.3.2.