Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and 2021. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Adding Quoted Words to an Index.
Written by Allen Wyatt (last updated February 21, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and 2021
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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13240) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Word here: Adding Quoted Words to an Index.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Putting together an index for your documents can be challenging, but Word provides some great tools to make the task ...
Discover MoreThere are several ways you can create an index in Word, but the first step is always to figure out what should go in the ...
Discover MoreWhen dealing with large projects, it is not uncommon to break the project into multiple documents. When it comes time to ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-03-27 22:40:12
Anonymous
How would you tweak this macro to create a list at the end but not delete anything, not modify the document?
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments