OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[solved] add multiple underlined words to dictonary macro

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Sat Jan 05, 2008 12:25 pm    Post subject: [solved] add multiple underlined words to dictonary macro Reply with quote

i need a macro to batch add all red underlined words in a writer document to the OOo dictionary.

in another forum my request was partially solved by a macro which colelcts all the red underlined words in a new writer document.

http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=1222&sid=6a2fbf91a94719b6e43f0f81b506dda2

i wonder if there's a way to create a new macro or to modify that macro to add all that words to dictionary with just 1-click

Code:

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


thanks for youy help


Last edited by Tommy27 on Wed Jan 23, 2008 1:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
probe1
Super User
Super User


Joined: 18 Aug 2004
Posts: 2060
Location: Chonburi Thailand Asia

PostPosted: Wed Jan 09, 2008 9:00 am    Post subject: Reply with quote

you already have the words ... so combine the code you have with the one from
add2dic
posted from me on 2005-04-10 12:30 in openoffice.general.german
Code:
Sub add2dic

' sichtbares Dokument (es ist besser eines da)
oDok = StarDesktop.getCurrentComponent()

' sichtbaren cursor
oViewCursor = oDok.getCurrentController().getViewCursor()

' textcursor an stelle des sichtbaren cursors
oCur = oViewCursor.getText().createTextCursorByRange(oViewCursor)

' wenn nur Einfügemarkierung
if oCur.IsCollapsed then
   ' Wort unter dem Cursor mit TC markieren

   if NOT oCur.isStartOfWord() then
       oCur.gotoStartofWord(FALSE)
   endif
      
   ' Wortanfang bis Ende markieren
   oCur.gotoEndofWord(TRUE)

   ' den Inhalt des TC
   sWort = oCur.getString
   
' Ausgewählter Text
else
   sWort = oCur.getString
end if

if Len(sWort) = 0 then
   exit sub
end if

' wir haben ein Wort
' nun zweiter Teil: Wörterbuch Service und hinein damit

' service
oWBListe = createUnoService ("com.sun.star.linguistic2.DictionaryList")

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

' ermitteltes Wort zum WB
' FALSE: kein negatives Wort
' : Ersetzungstext (bei neg. Worten)

' msgbox "Worte im WB: " & oWB.count
oWB.add(sWort, FALSE, "" )
' msgbox "Worte im WB: " & oWB.count

End Sub


Can you work it out?
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Wed Jan 09, 2008 12:24 pm    Post subject: Reply with quote

sorry,
i have no idea how to code a macro...

would you please be so kind to do it for me?

that would be a great imrpovemnt for your macro too
Back to top
View user's profile Send private message
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Wed Jan 16, 2008 12:32 am    Post subject: Reply with quote

anybody may help me to combine these macros?
Back to top
View user's profile Send private message
probe1
Super User
Super User


Joined: 18 Aug 2004
Posts: 2060
Location: Chonburi Thailand Asia

PostPosted: Wed Jan 16, 2008 2:59 am    Post subject: Reply with quote

Sorry - forgotten about you (lazy at the pool)...


These two lines initiate the access to the standard dictionary:
Code:

' service
oWBListe = createUnoService ("com.sun.star.linguistic2.DictionaryList")

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


Add to your original macro, I suggest just after
If Not HasUnoInterfaces
showstopper (of course after the END IF)


now add the line
oWB.add(sListaPalabras, FALSE, "" )

to the code where the string is written to the document:
Code:
  oListDocModel.Text.insertString (oTextCursor, sListaPalabras, False)
  oWB.add(sListaPalabras, FALSE, "" )



Untested (still 30°C - forgive me)
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Wed Jan 16, 2008 11:10 am    Post subject: Reply with quote

i tried to edit as you said but nothing happens...
i mean, the edit macro do the same thing as the original; it opens a new file with the underlined words but doesn0t add them to dictionary.

i think i didn't edit the code in the right way.

here's the code i wrote, can you see where's the mistake?

Code:
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
   
' 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
               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()
    oWB.add(sListaPalabras, FALSE, "" )

End Sub
Back to top
View user's profile Send private message
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Sun Jan 20, 2008 1:39 am    Post subject: Reply with quote

any idea?
Back to top
View user's profile Send private message
probe1
Super User
Super User


Joined: 18 Aug 2004
Posts: 2060
Location: Chonburi Thailand Asia

PostPosted: Wed Jan 23, 2008 2:22 am    Post subject: Reply with quote

Finally I found the time to have a look at your problem. You need only a minor adjustment:

add a line here:
Code:

[...]
                  If Not oSpellChk.isValid(oTextCursor.getString(), oTextCursor.getPropertyValue("CharLocale"), aProp()) Then
                    oWB.add( oTextCursor.getString() , FALSE, "" )
               sListaPalabras = sListaPalabras + oTextCursor.getString() + Chr(13)
[...]


and delete the statement oWB.add at end of your last posted code.
This works for me here (2.3.1/WinXP) - working for you, too?
_________________
Cheers
Winfried
My Macros
DateTime2 extension: insert date, time or timestamp, formatted to your needs
Back to top
View user's profile Send private message Visit poster's website
Tommy27
OOo Advocate
OOo Advocate


Joined: 18 Nov 2006
Posts: 257

PostPosted: Wed Jan 23, 2008 12:48 pm    Post subject: Reply with quote

GREAT, IT WORKS!!!

this macro kicks ass!!!!

thank you very much for you help!!!

I'm gonna post here the final code for other users who wanna copy and paste it in the OOo macro file. I'm gonna call this macro BatchAdd2Dic

Code:
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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group