(Sort of) search and replace

P

Pete

Hi - I'm writing a small macro to perform a modified search-and-replace, and I can do it using VBA string manipulation and concatenation functions, but I'm sure there is a shorter, simpler, smarter way of doing it with Word's built-in bookmarks and so on. I need to do a multiple seach and replace, for example looking for lines lik

xxxx_Something_****

and replace wit

xxxx_SomethingEls

In other words, if a line has _Something in it, I need to replace not just _Something, but _Something_**** with _SomethingElse. So it need to treat **** as wildcards. Any ideas? Thanks

Pete
 
J

Jezebel

Why not use search and replace with wildcards?

Search for: (xxxx_Something)_*^013
Replace with: \1^p



Pete said:
Hi - I'm writing a small macro to perform a modified search-and-replace,
and I can do it using VBA string manipulation and concatenation functions,
but I'm sure there is a shorter, simpler, smarter way of doing it with
Word's built-in bookmarks and so on. I need to do a multiple seach and
replace, for example looking for lines like
xxxx_Something_****

and replace with

xxxx_SomethingElse

In other words, if a line has _Something in it, I need to replace not just
_Something, but _Something_**** with _SomethingElse. So it need to treat
**** as wildcards. Any ideas? Thanks!
 
K

Klaus Linke

Find xxxx_Something_****
and replace with
xxxx_SomethingElse


Hi Pete,

If you do try wildcards and run into problems, we'd need to know what you
really have instead of xxxx, Something, SomethingElse, and ****.
For wildcards, you need some pattern (some rule that the parts xxxx ...
conform to).

To replace ABC with AD:
Find what: (A)BC
Replace with: \1D

You just re-use A. To do that, you put it in brackets, and use \1 (for the
1st bracket) in the replacement.

For any of the letters/parts ABCD above you can insert some wildcard
expression that matches that particular part.
If A is any number of "x"es, you'd use [x]@ instead of A, and so on.
Depending on what you really have, it can be trivial or very tricky to find
properly working expressions.

Regards,
Klaus
 
J

Jezebel

With Wildcards active,
search for: (Properties\\A\\Application)_*.htm
replace with: \1F1.htm




Pete said:
Thanks for your replies. This should be easy, but I'm stumped. Can
someone please tell me why this doesn't work? I'd be happy to use the UI or
API. I'm using Word 2003.
Sample doc:
IDH_AngleiMateDefinition_Application = Properties\A\Application_AngleiMateDefinition.htm
IDH_AngleConstraint_Angle = Properties\A\Angle_AngleConstraint.htm
IDH_AngleConstraint_Application = Properties\A\Application_AngleConstraint.htm

Required result:
IDH_AngleiMateDefinition_Application = Properties\A\ApplicationF1.htm
IDH_AngleConstraint_Angle = Properties\A\Angle_AngleConstraint.htm
IDH_AngleConstraint_Application = Properties\A\ApplicationF1.htm

Notice that lines 1 and 3 changed. Anything with the string
Properties\A\Application_*.htm needs to be changed to Properties\A\ApplicationF1.htm

I tried a million codes and tags to no avail. I wondered if the slash was
being misinterpreted, so in the search dialog I tried:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top