Get next spelling/grammar errors via the API

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Get next spelling/grammar errors via the API

Post by OlivierR »

Hi *,

Do you know a way to go to the next spelling/grammar error from the cursor via the API?

Or is there a method to list all spelling/grammar errors found in a document?
LibreOffice 6.4Windows 10
User avatar
einstein
Posts: 47
Joined: Sat Nov 05, 2016 1:45 am
Location: State of Mexico, México.

Re: Get next spelling/grammar errors via the API

Post by einstein »

Macros would not be necessary, since by pressing the F7 key we have everything.

Here is a possible example that maybe you could improve and adapt to your needs.
It's a macro for Writer:

Code: Select all

Sub SpellingError
  Dim aEmpty(0) As New com.sun.star.beans.PropertyValue
  Dim locale As New com.sun.star.lang.Locale
   locale.Language = "en"
    locale.Country = "US"
            SpellC = createUnoService("com.sun.star.linguistic2.SpellChecker")
  			 oText = ThisComponent.Text
           oCursor = oText.CreateTextCursor()
   oSentenceCursor = oText.CreateTextCursor()
  Do While oCursor.gotoNextWord(True)
    oSentenceCursor.gotoRange(oCursor.getStart(), False)
    Do While oSentenceCursor.gotoNextWord(True) AND oText.compareRegionEnds(oSentenceCursor, oCursor) >= 0
			  dato = Trim(oSentenceCursor.String)
'-----------------------------------------------------------------------------------------------------			  
			  dato = replace(dato, Chr(13) + Chr(10), "")
			  dato = replace(dato, ":","")
			  dato = replace(dato, ".","")
			  dato = replace(dato, ";","")
			  dato = replace(dato, ",","")
			  dato = replace(dato, "-","")
			  dato = replace(dato, "_","")
			  'ETC			  
'----------------------------------------------------------------------------------------------------			  
			  	oReturn = SpellC.isValid(dato, locale, aEmpty())
	    If oReturn = False Then
	    acum = acum & chr(13) & dato
	end if  			
   Loop
    	oCursor.goRight(0, False)
  	Loop
  			msgbox acum, , "MisSpellings"
End Sub
lo 5.1.6.2 | aoo 4.1.3 | win 7/10
All I know is that I know nothing
OlivierR
Posts: 38
Joined: Sun Jul 25, 2010 3:13 pm

Re: Get next spelling/grammar errors via the API

Post by OlivierR »

Thanks for the answer.

But I don’t want to parse anew a text that had already been parsed. What I’d like is to go to the next error already found. And BTW I’m much more interested by grammar errors than spelling mistakes.

I also need to list all errors. In LO, a grammar error is defined by a structure (https://www.openoffice.org/api/docs/com ... Error.html) that would allow to sort them.
LibreOffice 6.4Windows 10
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Get next spelling/grammar errors via the API

Post by RoryOF »

This module
http://www.openoffice.org/api/docs/comm ... le-ix.html
offers access to the proofreading Iterator.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Post Reply