[Solved] Add all underlined words to dictionary

Creating a macro - Writing a Script - Using the API

[Solved] Add all underlined words to dictionary

Postby Tommy » Fri Dec 28, 2007 4:13 pm

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.
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: add all underlined words to dictionary

Postby esperantisto » Sun Dec 30, 2007 10:45 am

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.
OOo 3.1 / Win XP / 2000 / openSUSE Linux 11.1
esperantisto
 
Posts: 137
Joined: Mon Oct 08, 2007 1:31 am

Re: add all underlined words to dictionary

Postby Tommy » Sun Dec 30, 2007 11:38 am

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   Expand viewCollapse view
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
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: add all underlined words to dictionary

Postby Tommy » Sun Dec 30, 2007 12:05 pm

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   Expand viewCollapse view
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-
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

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

Postby Tommy » Sun Jan 27, 2008 10:29 am

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   Expand viewCollapse view
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
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Postby Tommy » Mon Jan 28, 2008 11:55 am

if there's anybody interested in creating and auto-installing version with a toolbar button, feel free to use that code. :P
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Postby Tommy » Sat Feb 07, 2009 12:02 pm

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
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Postby Tommy » Sun Feb 08, 2009 4:17 pm

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.
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Postby Tommy » Sun Feb 15, 2009 12:15 pm

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

Code: Select all   Expand viewCollapse view
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?
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm

Re: [Solved] Add all underlined words to dictionary

Postby Tommy » Mon Mar 09, 2009 12:00 am

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   Expand viewCollapse view
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
User avatar
Tommy
 
Posts: 134
Joined: Sun Dec 23, 2007 2:44 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: liangshixing and 3 guests