Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag 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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of 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!
Avatar of 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!
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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...
But the macro only works on spelling mistakes that are not followed by a space as in your original question.
Avatar of 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?
Avatar of mrwad99

ASKER

Thanks for this.