Avatar of mrwad99
mrwad99
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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
Microsoft WordMicrosoft OfficeVB ScriptVBA

Avatar of undefined
Last Comment
mrwad99

8/22/2022 - Mon
GrahamSkan

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

mrwad99

ASKER
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!
mrwad99

ASKER
Eek, that has stopped working:

Highlighting all of

This contains splling mistakess as you can see.

doesn't change the style of anything!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mrwad99

ASKER
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...
GrahamSkan

But the macro only works on spelling mistakes that are not followed by a space as in your original question.
mrwad99

ASKER
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?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
mrwad99

ASKER
Thanks for this.