Page 1 of 1

[Solved] Writer – Highlight Spelling Errors

PostPosted: Sat Aug 05, 2017 3:11 pm
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   Expand viewCollapse view
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!

Re: Writer – Highlight Spelling Errors

PostPosted: Tue Aug 08, 2017 1:02 pm
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   Expand viewCollapse view
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.

Re: Writer – Highlight Spelling Errors

PostPosted: Tue Aug 08, 2017 1:15 pm
by REH-CAE-RPT
Works great from what I can tell – much appreciated!

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Sat Aug 26, 2017 5:41 am
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.

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Sat Aug 26, 2017 5:53 pm
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   Expand viewCollapse view
    [...]
    ' 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
[...]
 

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Tue Aug 29, 2017 2:12 am
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_(information)#Multi-dimensional_table

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Wed Aug 30, 2017 2:17 pm
by hubert lambert
Hope this one works:
highlight spell errors_3.odt
(19.4 KiB) Downloaded 73 times

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Wed Aug 30, 2017 2:48 pm
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?

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Wed Aug 30, 2017 3:43 pm
by hubert lambert
If I understand well, just add these two lines:
Code: Select all   Expand viewCollapse view
    lp = createUnoService("com.sun.star.linguistic2.LinguProperties")
    lp.setPropertyValue("IsSpellUpperCase", True)

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Wed Aug 30, 2017 4:01 pm
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...)

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Thu Aug 31, 2017 9:05 am
by hubert lambert
Why would you want this for spell checking ?

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Thu Aug 31, 2017 9:16 am
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.

Re: [Solved] Writer – Highlight Spelling Errors

PostPosted: Thu Aug 31, 2017 10:03 am
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 84 times