Click here to Skip to main content
15,887,477 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Automatically Format Foreign Words in a Word Document

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Jun 2015CPOL1 min read 6.7K   2  
Many professionals have to use foreign-language terms in their documents (e.g. English terminology); this macro automatically formats these terms.

Introduction

I am always very annoyed at having to write a document with a great deal of foreign-language terminology. It looks very bad and clumsy unless you do something that highlights these words, for example in Dutch:

De targets die de Raad van Bestuur stelde waren off limits.

I just wrote a Word macro that can do this automatically for you, The example works for Dutch text and highlights all words that are not recognized as Dutch, but as US/UK English, but it is very easy to adapt to whatever set of languages you would like.

Using the Code

Create a new macro in Word and paste in the following code (tested in version 2010). When you run the macro, all words in your document that are not Dutch, but are US/UK English, are formatted in italics, unless they were already italic, in which case they are formatted normally (assuming that surrounding words are in italics, so we highlight the word in question by making it normally formatted).

VB.NET
Sub ItalicizeEnglish()

If Application.Documents.Count < 1 Then
    MsgBox "No documents are open!"
    Return
End If

ActiveDocument.Content.LanguageID = msoLanguageIDDutch
ActiveDocument.Content.NoProofing = False

For Each w In ActiveDocument.SpellingErrors
    If Not ItalicizeIfInLanguage(w, msoLanguageIDEnglishUS) Then
        b = ItalicizeIfInLanguage(w, msoLanguageIDEnglishUK)
    End If
Next

End Sub

Function ItalicizeIfInLanguage(w, lan) As Boolean
    oldlan = w.LanguageID
    w.LanguageDetected = False
    w.LanguageID = lan
    If w.SpellingErrors.Count = 0 Then
        w.Font.Italic = Not w.Font.Italic
        ItalicizeIfInLanguage = True
    Else
        w.LanguageID = oldlan
        ItalicizeIfInLanguage = False
    End If
End Function

Points of Interest

This is not a very exciting piece of code. I wrote it in 30 minutes with no prior knowledge of VBA, but it is probably very useful. At least for me. :)

History

  • Version 1.0 was uploaded on June 24, 2015

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --