[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].
OpenOffice 5.2.7.2 (x64) on Windows 7 Professional
REH-CAE-RPT
 
Posts: 5
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 4 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 40
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!
OpenOffice 5.2.7.2 (x64) on Windows 7 Professional
REH-CAE-RPT
 
Posts: 5
Joined: Wed Jul 12, 2017 9:28 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 11 guests