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.
Written by Allen Wyatt (last updated June 25, 2018)
This tip applies to Word 97, 2000, 2002, and 2003
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:
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Having problems when it comes to replacing information in URLs? You're not the only one; it can be confusing making mass ...
Discover MoreDo you need to replace text with a term or phrase that uses multiple formats? You can perform this seemingly complex task ...
Discover MoreSmart quotes look great in a document, but may not be right for all instances of quote marks or apostrophes. If you need ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-08-23 22:31:43
Christine
my understanding that this works only if you know what the text is that you are looking for. I want to find all strings that have matching quotes to apply a quote style, how would I do that
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.
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 © 2024 Sharon Parq Associates, Inc.
Comments