VBA and Word: Searching for strings with non-alpha characters

Thursday, July 26th, 2012 | Microsoft Office 2010, Technical | Mike Smith

I ran across an issue with a client regarding VBA code.  Client was searching for “Email:  user@firm.com” and replacing it with a different email address.  The code would not always find the whole string, or only replace some of the items.  In reviewing the code, the ultimate solution was simple to deploy.

Current search string:  “EMAIL:  *@firm.com”

Issue: When searching for strings using VBA some characters (mostly symbols) are not literal even when put into a quotations as above.  In VBA the first step is to set “Find.MatchWildcards = True” in your code.

I experimented with different variations on the string, using some info about the special characters in Word searching (http://word.mvps.org/faqs/general/usingwildcards.htm):

“EMAIL*@firm.com”

“EMAIL:*.com”

“EMAIL: *?firm.com”

“EMAIL? *[@]firm[.]com”

None of the above seemed to help.  From there I thought I should try to remove ALL symbols from my search string and see if that made life easier.  I tried: “EMAIL*COM” and VOILA! String matched every time.

Moral of this story is to remove as much complication as possible from your search string and narrow down to specifics as needed.

Tags: , , , , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Page 1 of 11

Leave a Reply