Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Oct 2003
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find and Replace (2003)

    I have what ought to be a simple problem, but suspect that it may be complex or impossible (usually the case with Office products).

    I have a large file in which I need to search for all occurrences of an asterisk followed by a word in uppercase and then replace this by the word without the asterisk. (i.e., replacing *ONE, *TWO, *THREE, with ONE, TWO, THREE, but retaining *one, etc.). I can't simply delete all asterisks, as there are legitimate asterisks elsewhere in the file that I want to keep. I can search for any character or letter following an asterisk, but can't seem to make this case sensitive (match case isn't appropriate since I am searching for a 'special' character). Also, it seems to be impossible to replace with just the text - I can replace with whatever is in the Find what box, but that is no help. I can't seem to see a way to do it with wildcards, as the replace box doesn't accept the same wildcards as can be put in the find box.

    Short of going through the file and doing it manually, does anyone know a way of doing this with find and replace? Am I missing something very obvious?

  2. #2
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace (2003)

    Hi John,

    Try this with "Use wildcards" checked:

    Find What:
    <pre>*([A-Z]@ )</pre>

    Replace with:
    <pre>1</pre>


    Note there's a space between the "@" and the ")".

    In the test document I used, it met the criteria you described, but you may need to tweak it for you actual document.

    HTH!

  3. #3
    New Lounger
    Join Date
    Oct 2003
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace (2003)

    Many thanks for this. I would never have got to that on my own. This seems to pick up the bulk of the cases in my file, though it jumps over a few for some reason. It is certainly preferable to doing it manually.

  4. #4
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace (2003)

    Word wildcards aren't bad, but sometimes they just can't handle a particular match. I've also had trouble getting reliable matches when working with word boundaries; the matches are often much more "greedy" than they ought to be.

    If you set a reference to "Microsoft VBScript Regular Expressions 5.5" from Tools -> References in the VBA editor, you can use VBScript's RegExp object, which is much more predictable and flexible. The downside is that it's tricky business to do actual substitions without losing formatting.

    For anyone interested, here's how to perform John's replacement using the RegExp object, without losing any character formatting:

    <pre>Sub FixStarredWords()
    Dim re As RegExp
    Dim mtchs As MatchCollection
    Dim m As Match
    Dim para As Paragraph
    Dim rng As Range

    Set re = New RegExp

    re.Pattern = "*([A-Z]+s)"
    re.IgnoreCase = False
    re.Global = False

    For Each para In ActiveDocument.Paragraphs
    Do While re.Test(para.Range.Text)
    Set mtchs = re.Execute(para.Range.Text)
    For Each m In mtchs
    Set rng = para.Range
    With rng
    .Find.ClearFormatting
    .Find.Text = m.Value
    .Find.Execute
    If (.Find.Found) Then
    .Text = re.Replace(.Text, "$1")
    End If
    End With
    Next m
    Loop
    Next para
    </pre>


    The pattern isn't much different from the Wildcard pattern in this case, but as soon as you get into more complex patterns, I'll take regular expressions over Word wildcards any day.

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find and Replace (2003)

    Hi John:
    You might be better off using
    *([A-Z]{1,})

    The <code>{1,}</code>is functionally the same as <code>@</code> except {1,} is greedy & @ is parsimonious. Then you can pick up "*ONE." & not pick up "*One." I'm not sure it's what you want, though.
    Cheers,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •