Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro to replace every other instance of a string

Status
Not open for further replies.

tbarthen

Technical User
Jul 26, 2003
33
US
Hey, if anyone can help, I'm trying to come up with a macro for MS Word (Office XP version) that will replace every other instance of a string in a document, and then replace the remaining instances of that string with a different value.

Specifically, I have a document that is * delimited. I would like to replace every other star with a pipe ( | ). Then I would like to follow that with a global replace of the remaining stars to a new line character.

FYI: I've done a lot of Excel macros, but I'm not really familiar with Word macros much at all.

 
Hi tbarthen,

You should be able to do this with a search and replace.

Wildcards and pattern matching are a bit awkward sometimes and I couldn't make Word 2000 find [purple]\**\*[/purple] which I think ought to mean anything bounded by asterisks, but it did take [blue]\*?{1,}\*[/blue] which is one or more characters bounded by asterisks. So ..

Find [blue](\*)(?{1,})(\*)[/blue]

Replace with [blue]|\2^p[/blue]

.. and you should have what you want.

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
If I understand you right, you're saying that I should bring up the replace dialog in Word (control-h), and in the

"Find what" space, I should enter: (\*)(?{1,})(\*)

and in the "Replace with" space, I should enter: |\2^p


I did that, and it doesn't find anything to replace. Am I reading your explanation correctly?

 
Hi tbarthen,

You need to check the Use Wildcards box after you have selected More on the Find & Replace window.

Good Luck!
Peter Moran
 
Hey, that's pretty cool. I never noticed that wildcard option. Thanks a lot for your help guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top