This documentation is archived and is not being maintained.

Adding Auto Hyphenation in Word 2010

Office 2010

Office Quick Note banner

Handy Programming Tips for Microsoft Word 2010: Learn how to add auto-hyphenation to a Microsoft Word 2010 document.

Last modified: May 14, 2012

Applies to: Office 2010 | VBA | Word 2010

In this article
Add a Standard Module to a Word Document
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   May 2011

Provided by:    Frank Rice, Microsoft Corporation

By default, if you type a word that does not fit on a given line, Microsoft Word leaves the word intact and moves it to the next line. That can make your paragraphs look choppy and uneven. You can enable hyphenation by using commands on the ribbon or by using code. In this topic, you programmatically add automatic hyphenation to a document. To understand how to turn on auto hyphenation in a document, see Insert a hyphen. To complete this task, you must do the following:

In this task, you open a Word 2010 document, open the Visual Basic Editor, and then insert a standard module.

To add a standard module to a Word document

  1. Start Word 2010.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note Note

    If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.

  3. On the Insert menu, click Module. This adds Module1 to the Projects pane on the left side of the Visual Basic Editor.

In this task, you add programming code that enables auto-hyphenation in a document. It also converts automatic hyphens into actual hyphens.

To add code to the Visual Basic Editor

  1. In the Projects pane, click Module1.

  2. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub AutoHyphenDemo()
        With ActiveDocument
            ' Make the margins a little larger than normal.
            .PageSetup.LeftMargin = 100
            .PageSetup.RightMargin = 100
            ' Make the text justified so that the hyphens stand out more:
            .Range.ParagraphFormat.Alignment = wdAlignParagraphJustify
            ' Turn on auto-hyphenation.
            .AutoHyphenation = True
            ' In Word, try to select just a hyphen at the end of a line:
            ' you cannot because the hyphens are not really there. Word has
            ' inserted these automatically. You might want actual hyphens in your
            ' text, and the following line of code converts automatic
            ' hyphens into actual hyphens:
            ' Note that you can now select a hyphen, as if you had inserted
            ' it yourself. Make a mental note where one of the hyphens is,
            ' and the next block of code changes the margins. Note the
            ' behavior.
            .PageSetup.LeftMargin = 15
            .PageSetup.RightMargin = 15
            ' Show all content in the window, and find the hyphens, which now
            ' might not appear as hyphens. They are still there!
            ActiveWindow.ActivePane.View.ShowAll = True
            ' Put the margins back the way they were, and notice the hyphen behavior:
            ' your hyphens are back!
            .PageSetup.LeftMargin = 100
            .PageSetup.RightMargin = 100
        End With
    End Sub

In this task, you add text to the document and then step through the VBA code that adjusts the margins and enables auto-hyphenation. The code also turns auto-hyphens into actual hyphens so that you can see the difference between the two types.

To step through the code

  1. Open the document and type the following command (without the quotation marks) to add five paragraphs of text.


  2. Drag the Visual Basic Editor window to the right side of your monitor.

  3. Drag the Word window to the left side of your monitor and adjust the windows until you can see them both.

  4. Read through the comments as you work through the code so that you can interact with the document and code.

  5. Place the cursor in the AutoHyphenDemo module and press F8 to step through the code line-by-line.