Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. 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: Finding Quoted Text in VBA.

Finding Quoted Text in VBA

Written by Allen Wyatt (last updated June 2, 2018)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


Jennifer needs a way, in a macro, to find a string surrounded by either smart or straight quotes (or a mix of the two). She can't seem to find the proper mix of commands for the Find method to locate all instances of such text in all versions of Word.

It is important to be clear about what is being searched. The assumption in this tip is that your macro requires to you search for a specific string surrounded by quotes, not any string surrounded by quotes. For example, in a document that contains two quoted strings such as "my quoted text" and "more quoted text," you only want to find one of the strings for which you know the text, ahead of time, not both of the strings.

In this case, it is just fine to use the Find method, as you note. The question is how to accommodate the possibility of both smart quotes and straight quotes in what you seek. Fortunately, the Find method, by default, matches both straight and smart quotes interchangeably. The key point is knowing how to specify that you want the quote marks included in the search. The following code snippet should do the trick:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = """my quoted text"""
    .Format = False
    .MatchWildcards = False
End With
Selection.Find.Execute

The key in this code is how information is assigned to the .Text property. Note that whatever you are searching for (in this case, "my quoted text") is surrounded by three quotes on each side. The reason for this is rather arcane: The string you are searching for must be enclosed with quote marks; this is required by VBA. This is shown here:

"my quoted text"

Since you want an actual quote character at the beginning of what you are seeking, you need to include a second quote mark as a "delimiter" to indicate you want to find the quote mark character. This means there are now three quote marks at the beginning:

"""my quoted text"

The same extra-quote-mark-as-delimiter technique also applies to the end of the string, so you end up with what is shown in the code snippet. When the code is executed, Word dutifully finds the string, surrounded by any mix of straight or smart quotes, as desired.

It should also be noted that this approach only works if you are doing a regular search, with the .MatchWildcards property set to False.

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 (11639) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Finding Quoted Text in VBA.

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

Opening a Word Document when Starting the Computer

Some people use their computers for little else, other than to work on Word documents. If that is the case with you, then ...

Discover More

Adding Column Headings to a Table of Contents

Word makes it easy to create a Table of Contents. If you want column headings in that table, getting them takes a bit of ...

Discover More

Creating a Document Font List

If you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

When Replace Doesn't Work

Find and Replace is a great tool, but what are you to do if your find or replace doesn't work as you expect? This tip ...

Discover More

Replacing a Colon in a Sequence

Sometimes you'll run across the need to replace a very specific sequence of characters in your document. It is for these ...

Discover More

Easily Finding Superscripts

Word has a powerful Find and Replace capability, but sometimes it can be tricky to figure out exactly how to use it. Here ...

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 2 + 8?

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 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.

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.