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: Finding Quoted Text in VBA.

Finding Quoted Text in VBA

by Allen Wyatt
(last updated June 5, 2015)

3

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.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11638) 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: 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

Formatting a Cover Page

Formal reports look better when they are set up with an introductory cover page. Here's how you can add a cover page in a ...

Discover More

Pasting a Hyperlink

Need a quick link within a document to some external data? You can paste information so that Excel treats it just like a ...

Discover More

Selecting Default Paragraph Formatting

Want to return a paragraph's formatting back to it's pristine, unaltered state? You can do so by using the shortcut described ...

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 (MENU)

Saving Find and Replace Operations

Want to repeat the same Find and Replace operation over and over again? Here are a couple of ways you can improve your ...

Discover More

Replacing an X with a Check Mark

In order to provide a finishing touch to your document, you may want to replace mundane X marks with fancier check marks. ...

Discover More

Finding the Previous Occurrence

Using Word's Object Browser, it is very easy to move among different instances of what you want to search in your document. ...

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 for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 nine more than 2?

2012-04-30 17:32:25

Ken Endacott

Correction to previous posting.

The line:
aRange.End = aWord.End - 2
should read:
aRange.End = aWord.End - Len(aWord)

A leading quote is a single character word but a trailing quote is the first character of a word that includes following spaces or punctuation characters. The original line above only works if there is one space between the trailing quote and the next word. Note that the function Asc() operates on the first character if the argument is a multi-character word.

The routine will work with straight quotes, smart quotes or a mixture. You should start with the cursor outside quoted text and it will find the text within the next pair of quotes.


2012-04-30 16:42:58

Ken Endacott

Sub QuotedFind()
Dim aWord
Dim aRange As Range
Dim testRange As Range
Dim aFlag As Boolean
aFlag = False
Set testRange = ActiveDocument.Range
testRange.Start = Selection.Range.Start
For Each aWord In testRange.Words
If Asc(aWord) = 34 Or Asc(aWord) = 147 Or Asc(aWord) = 148 Then
If aFlag Then
aRange.End = aWord.End - 2
aRange.Start = aRange.Start + 1
aFlag = False
aRange.Select
If MsgBox("Continue?", vbYesNo) = vbNo Then Exit Sub
Else
aFlag = True
Set aRange = aWord
End If
End If
Next aWord
End Sub


2012-04-30 08:58:54

Jennifer Thomas

Thanks for the advice; actually, I do want to search for any text in quotes -- the point is to find quoted text and then apply formatting to that text.

As you point out, it doesn't work with Wildcards on, so we used to do this with character codes, but those don't work in 2010.

If anyone can help with a macro that would find any text in quotes, I'd really appreciate it!

Thanks.


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

Links and Sharing
Share