[Solved] Writer – Highlight Spelling Errors

Creating a macro - Writing a Script - Using the API

[Solved] Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Sat Aug 05, 2017 3:11 pm

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!
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
REH-CAE-RPT
 
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: Writer – Highlight Spelling Errors

Postby hubert lambert » Tue Aug 08, 2017 1:02 pm

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.
Attachments
highlight spell errors.odt
(33.61 KiB) Downloaded 24 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Tue Aug 08, 2017 1:15 pm

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

Postby REH-CAE-RPT » Sat Aug 26, 2017 5:41 am

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
REH-CAE-RPT
 
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Postby hubert lambert » Sat Aug 26, 2017 5:53 pm

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
[...]
 
Attachments
highlight spell errors_2.odt
(31.94 KiB) Downloaded 23 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Tue Aug 29, 2017 2:12 am

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
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

Postby hubert lambert » Wed Aug 30, 2017 2:17 pm

Hope this one works:
highlight spell errors_3.odt
(19.4 KiB) Downloaded 17 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Wed Aug 30, 2017 2:48 pm

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
REH-CAE-RPT
 
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Postby hubert lambert » Wed Aug 30, 2017 3:43 pm

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)
Attachments
highlight spell errors_3.odt
(19.57 KiB) Downloaded 16 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Wed Aug 30, 2017 4:01 pm

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
REH-CAE-RPT
 
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Postby hubert lambert » Thu Aug 31, 2017 9:05 am

Why would you want this for spell checking ?
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am

Re: [Solved] Writer – Highlight Spelling Errors

Postby REH-CAE-RPT » Thu Aug 31, 2017 9:16 am

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
REH-CAE-RPT
 
Posts: 14
Joined: Wed Jul 12, 2017 9:28 pm

Re: [Solved] Writer – Highlight Spelling Errors

Postby hubert lambert » Thu Aug 31, 2017 10:03 am

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 29 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 59
Joined: Mon Jun 13, 2016 10:50 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 9 guests