Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Adding Quoted Words to an Index.

Adding Quoted Words to an Index

Written by Allen Wyatt (last updated July 9, 2022)
This tip applies to Word 97, 2000, 2002, and 2003


Mike has a document that contains quite a few words surrounded by quote marks. He would like the quoted words to be marked for inclusion in an index and for the quote marks to be removed.

The best way to make such a change in your document is through the use of a macro. The macro needs to search for the quoted text, figure out where it starts and stops, delete the quote marks, and then add the index mark. (The mark is added through the use of an XE field.)

There are probably many ways that such a macro could be constructed. The following is one that could be used:

Sub QuotesToIndexEntries()
    Dim iOpenQuote As Integer
    Dim iCloseQuote As Integer
    Dim sP As String
    Dim sPhrase As String
    Dim iParas As Integer

    If Selection.ExtendMode Then Exit Sub

    iParas = Selection.Paragraphs.Count
    Selection.StartOf unit:=wdParagraph, Extend:=wdMove
    For J = 1 To iParas
        Selection.MoveEnd unit:=wdParagraph
        sP = Selection.Text
        iOpenQuote = InStr(sP, Chr(34))
        If iOpenQuote = 0 Then iOpenQuote = InStr(sP, Chr(147))

        While iOpenQuote > 0
            iCloseQuote = InStr(iOpenQuote + 1, sP, Chr(34))
            If iCloseQuote = 0 Then
                iCloseQuote = InStr(iOpenQuote + 1, sP, Chr(148))
            End If
            If iCloseQuote > 0 Then
                sPhrase = Mid(sP, iOpenQuote + 1, _
                  iCloseQuote - iOpenQuote - 1)
                Selection.Collapse Direction:=wdCollapseStart
                Selection.MoveRight unit:=wdCharacter, _
                  Count:=iOpenQuote - 1, Extend:=wdMove
                Selection.Delete unit:=wdCharacter, Count:=1
                Selection.MoveRight unit:=wdCharacter, _
                  Count:=Len(sPhrase), Extend:=wdMove
                Selection.Delete unit:=wdCharacter, Count:=1

                Selection.Fields.Add Range:=Selection.Range, _
                  Type:=wdFieldEmpty, _
                  PreserveFormatting:=False
                Selection.MoveLeft unit:=wdCharacter, Count:=1
                Selection.Delete unit:=wdCharacter, Count:=2
                Selection.TypeText Text:="XE " + Chr(34) + _
                  sPhrase + Chr(34)
                Selection.MoveRight unit:=wdCharacter, _
                  Count:=1, Extend:=wdMove
            End If
            Selection.MoveEnd unit:=wdParagraph
            sP = Selection.Text
            iOpenQuote = InStr(sP, Chr(34))
            If iOpenQuote = 0 Then iOpenQuote = InStr(sP, Chr(147))
        Wend
        
        Selection.MoveStart unit:=wdParagraph, Count:=1
    Next J
End Sub

In order to use the macro, just select the text you want to affect prior to running it. (If you want to affect the entire document, press Ctrl+A before running the macro.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (3804) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Adding Quoted Words to an Index.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Doubling Your Money

Make your money last longer by using your head when printing labels. Here's a great example of how you can double the ...

Discover More

Printing Portions of Mail Merged Documents

When you use a data source to create a bunch of documents in a mail merge, you might not want to print all the documents ...

Discover More

Counting with Formulas

When you need to count a number of cells based upon a single criteria, the standard function to use is COUNTIF. This tip ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Improper Index Page Numbers

Adding an index to a document can be a nice finishing touch, particularly if the document is a long one. What happens if ...

Discover More

Specifying an Index Page-Range Separator

When generating an index, Word normally uses a dash to indicate page ranges. You can change the character used for these ...

Discover More

Specifying an Index Entry Separator

One of the switches you can use with the index field allows you to specify how index entries should be separated from the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.