Written by Allen Wyatt (last updated April 3, 2020)
This tip applies to Word 97, 2000, 2002, and 2003
If you are writing macros, you probably do a lot of work with string variables. A common operation related to string variables is the need to replace one occurrence of characters in a string with another occurrence. For instance, consider the following string:
"This is my string of characters."
You might want to replace "of" with another word, such as "that has". The following general-purpose function can do just that, and much more:
Function RepText(sIn As String, sFind As String, sRep As String) As String Dim x As Integer x = InStr(sIn, sFind) While x > 0 sIn = Left(sIn, x - 1) & sRep & Mid(sIn, x + Len(sFind)) x = InStr(sIn, sFind) Wend RepText = sIn End Function
As an example of how to use the function, you can do the following:
sTemp = "This is my string of characters." sTemp = RepText(sTemp, "of", "that has")
When this code is executed, sTemp will contain the string "This is my string that has characters."
In my routines, I often utilize a function just like RepText to remove extra spaces (replacing a two-space string with a single-space string) or replacing multiple spaces with a tab character. How you use it, of course, is entirely up to you.
Those who have used only the latest versions of VBA may be curious as to why I would use a function such as RepText rather than the built-in Replace function. While the Replace function does essentially the same thing as RepText, its addition to the VBA arsenal is a relatively new occurrence. If your macros have even the slightest possibility of being used in older versions of Word, then using Replace runs the risk of crashing your macro—it won't be available in those versions.
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 (811) applies to Microsoft Word 97, 2000, 2002, and 2003.
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!
You can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a ...
Discover MoreNeed to know if the Num Lock key is on or off? You can use a short bit of macro code to figure out the state of the key.
Discover MoreWhen comparing two pieces of text, you may find that Word's smart quotes can mess up the comparison. Here's a quick way ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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 © 2025 Sharon Parq Associates, Inc.
Comments