[Solved] Writer – Highlight Spelling Errors

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

[Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

Does anybody know a way to highlight all spelling errors within a document, as opposed to the default underline/red squiggle?

The VBA macro would be:

Code: Select all

Selection.HomeKey Unit:=wdStory
    Dim oSE As Range, oGE As Range
    For Each oSE In ActiveDocument.SpellingErrors
        oSE.HighlightColorIndex = wdYellow
    Next
lbl_Exit:
    Exit Sub
End Sub
Thanks for any help!
Last edited by Hagar Delest on Tue Aug 08, 2017 10:00 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Writer – Highlight Spelling Errors

Post by hubert lambert »

Hello,
REH-CAE-RPT wrote:Does anybody know a way to highlight all spelling errors within a document
Here is a first attempt:

Code: Select all

sub highlight_spell_errors(optional container)
    if ismissing(container) then
        container = thiscomponent
    end if

    ' get text object
    T = container.Text

    ' get text cursor
    cursor = T.createTextCursor()

    ' get spell checker service
    lsm = createUnoService("com.sun.star.linguistic2.LinguServiceManager")
    spellchecker = lsm.getSpellchecker()

    ' bypassing an uno api bug which confuse between XSpellChecker and XSpellChecker1
    reflector = createUnoService("com.sun.star.reflection.CoreReflection")
    xspellchecker = reflector.forName("com.sun.star.linguistic2.XSpellChecker")
    isvalid = xspellchecker.getMethod("isValid")

    ' iterate through all words
    dim props() as new com.sun.star.beans.PropertyValue
    do
        cursor.gotoStartOfWord(False)
        cursor.gotoEndOfWord(True)
        word = cursor.String
        if not isValid.invoke(spellchecker, array(word, cursor.CharLocale, props())) then
            cursor.CharBackColor = 16776960
        end if
        cursor.gotoNextWord(False)
        if cursor.isEndOfParagraph then
            cursor.gotoNextParagraph(False)
        end if
    loop while T.compareRegionEnds(T, cursor) <> 0

    ' check sub texts
    frames = container.TextFrames
    for each frame in frames
        on error resume next
        highlight_spell_errors(frame)
    next frame
    tables = container.TextTables
    for n = 0 to tables.Count -1
        table = tables(n)
        cellnames = tables(n).CellNames
        for each cellname in cellnames
            cell = table.getCellByName(cellname)
            highlight_spell_errors(cell)
        next cellname
    next n
end sub
It shall exist a simpler way, but I can't figure out how.
In addition, it seems that there's a bug with the spell checker api service, as interfaces XSpellChecker and XSpellChecker1 are conflicting...
Do not expect any great performance over large document.
Attachments
highlight spell errors.odt
(33.61 KiB) Downloaded 188 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

Works great from what I can tell – much appreciated!
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

Sorry to post in a solved topic, but is there any way to apply this macro to an entire document, regardless of whether it contains cells/tables? (Or would that require a separate macro altogether?)

When I try to apply this from text that contains cells/tables, it has the error message: "BASIC runtime error. Sub-procedure or function procedure not defined." and highlights the fourth-last line: "highlight_spell_errors(cell)"

Thanks for any help.
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Post by hubert lambert »

I can't reproduce. Could you please upload a file where this error happens ?
 Edit: I had infact some problems with AOO, due to erroneous basic syntax.
Could you please try this new one? The change is in the do...loop loop:

Code: Select all

    [...]
    ' iterate through all words
    dim props() as new com.sun.star.beans.PropertyValue
    do while cursor.gotoNextWord(False)
        cursor.gotoEndOfWord(True)
        word = cursor.String
        if not isValid.invoke(spellchecker, array(word, cursor.CharLocale, props())) then
            cursor.CharBackColor = 16776960
        end if
        cursor.collapseToEnd(False)
    loop
[...]
 
Attachments
highlight spell errors_2.odt
(31.94 KiB) Downloaded 193 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

Thanks. I've tried the new code but had the same issue.

One of the occasions I'd use this involves copying/pasting text from websites containing tables – the Age Table here, for example:
https://en.wikipedia.org/wiki/Table_(in ... onal_table
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Post by hubert lambert »

Hope this one works:
highlight spell errors_3.odt
(19.4 KiB) Downloaded 198 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

This works! Thanks so much, I do appreciate it.

Um, OK, one more (hopefully) minor thing: What's the easiest way to include a case change so the entire document is in lower case before the highlighting is applied?
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Post by hubert lambert »

If I understand well, just add these two lines:

Code: Select all

    lp = createUnoService("com.sun.star.linguistic2.LinguProperties")
    lp.setPropertyValue("IsSpellUpperCase", True)
Attachments
highlight spell errors_3.odt
(19.57 KiB) Downloaded 177 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

Hmm. That didn't seem to do it. Sorry, I didn't explain very well.

I'm just hoping to simulate doing this:

Ctrl + A
Shift + F3
Shift + F3
Shift + F3

...before applying the macro.

(Although, Ctrl + A seems to highlight specific cells rather than the entire document, depending on where the cursor is...)
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Post by hubert lambert »

Why would you want this for spell checking ?
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
REH-CAE-RPT
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Post by REH-CAE-RPT »

It's complicated... But a lot of the time the text I'm searching is entirely in upper case. Changing it to entirely lower case and highlighting the spelling errors enables me to find words that (usually) should be proper nouns, as well as general spelling errors.
LibreOffice 5.2.7.2 (x64) on Windows 7 Professional
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Post by hubert lambert »

We have for this to use the "dispatcher", i.e. the same commands used from the graphic interface (and that you can catch with the macro recorder).
Here it is:
highlight spell errors_3.odt
(19.22 KiB) Downloaded 231 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Post Reply