Auto apply style within selected text to mispelled words

Ah hello.

When studying, I note down a lot of what I learn in a word document.  This contains code snippets, which word often underlines as spelling mistakes as it doesn't understand them.  I manually highlight each occurrence and apply a style called "Inline code", which has a different font, and spell checking disabled, to make it stand out as "code".

To speed things up, I would like to be able to highlight a section of text, and have word automatically apply this style called to all spelling mistakes.  Furthermore, I would like this to be extended so that if I have, say, the following:

callFunction(int a, int b);

...then the style is applied to everything up to the terminating semi colon, not just the callFunction part.  I think the logic for this would be

for each spelling mistake:
  if there is not a space after the last character of the misspelled word
     continue applying the style to each character until we hit a semi colon

Could anyone possibly advise on this?

I am using Word 2007 and 2010.  Note I think macros are written in VBS, but I really am not sure on that.  Hence the 500 points :)

TIA
LVL 19
mrwad99Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
See if this VBA macro does what you need
Sub ApplyCodeStyle()
    Dim spErr As Range
    Dim rng As Range
    Dim para As Paragraph
    
    For Each spErr In Selection.Range.SpellingErrors
        Set rng = spErr.Duplicate
        rng.Collapse wdCollapseEnd
        rng.MoveEnd wdCharacter, 1
        If rng.Text <> " " Then
            rng.End = rng.Document.Range.End
            With rng.Find
                .Text = ";"
                If .Execute Then
                    rng.Start = spErr.Start
                    rng.Style = "Inline code"
                End If
            End With
        End If
    Next spErr
End Sub

Open in new window

mrwad99Author Commented:
Thank you.  Can I request a small improvement please?

If the misspelled word is immediately followed by an opening parenthesis, or has a single space then an opening parenthesis, the style is applied to everything up to an including the final parenthesis.  EG, assuming bold is the "inline code" style:

Here are examples of function calls: myFunc(), myFunc (1, 2), the latter having parameters.

would be

Here are examples of function calls: myFunc(), myFunc (1, 2), the latter having parameters.

Thanks in advance!
mrwad99Author Commented:
Eek, that has stopped working:

Highlighting all of

This contains splling mistakess as you can see.

doesn't change the style of anything!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GrahamSkanRetiredCommented:
Here is a modified macro. The code is staring to look a bit clunky.
Sub ApplyCodeStyle()
    Dim spErr As Range
    Dim rng As Range
    Dim para As Paragraph
    Dim strStyleName As String
    
    strStyleName = "Inline Code"
    
    For Each spErr In Selection.Range.SpellingErrors
        Set rng = spErr.Duplicate
        rng.Collapse wdCollapseEnd
        rng.MoveEnd wdCharacter, 1
        If rng.Text <> " " Then
            If rng.Text = "(" Then
                rng.End = rng.Document.Range.End
                With rng.Find
                    .Text = ")"
                    If .Execute Then
                        rng.Start = spErr.Start
                        rng.Style = strStyleName
                    End If
                End With
            Else
                rng.End = rng.Document.Range.End
                With rng.Find
                    .Text = ";"
                    If .Execute Then
                        rng.Start = spErr.Start
                        rng.Style = strStyleName
                    End If
                End With
            End If
        Else
            rng.MoveEnd wdCharacter, 1
            If rng.Text = " (" Then
                rng.End = rng.Document.Range.End
                With rng.Find
                    .Text = ")"
                    If .Execute Then
                        rng.Start = spErr.Start
                        rng.Style = strStyleName
                    End If
                End With
            End If
        End If
    Next spErr
End Sub

Open in new window


I don't quite understand your last comment (40748008). Surely it is doing what you want?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrwad99Author Commented:
Thanks Graham.

Further to my comment, I would expect that it changed it to

This contains splling mistakess as you can see

...since there are two misspelled words.

Sorry if I am not being clear...
GrahamSkanRetiredCommented:
But the macro only works on spelling mistakes that are not followed by a space as in your original question.
mrwad99Author Commented:
Bah, yes I see what's going on.

The space requirement was to cover complex statements like

Here is a pointer in action: myObjectPointer->m_i = 100;, which sets a member to 100.

The parenthesis bit was to cater for function calls, as in

Here are examples of function calls: myFunc(), myFunc (1, 2), the latter having parameters.

However nothing caters for simple expressions, such as

STL has an automatic pointer template class, auto_ptr is its name

...which is why I made my last comment.  Sorry.

I tried to "hack" your code by placing the original script you posted at the end of the last one, in an attempt to reapply the style to *every* misspelled word.  I removed the test for a blank space, which I could figure out, but I can't see how to remove the test for a semi colon.

Would you be so kind as to make this final addition?
mrwad99Author Commented:
Thanks for this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.