Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search and Replace Oddity (Word XP)

    Hi. I'm totally befuddled by this one.
    I am modifying some code I had written to combine strings in a document. Specifically, these are reference numbers entered in curly braces, i.e. {10} etc. If the author has entered something like {4} {10}, I want to combine them into {4, 10} for ease of reading.
    Anyway, in VBA, I tell Word to find the string "}{", intending to replace it with ", ". But when I run or step through the code, I find that it wants to search for a zero! (I can verify this by stopping the code and checking the values in the Find dialog box.) I can't find any explaination for this at all. It happens whether I use a string variable for the search criteria or whether I expclitly state .text = "}{".
    I'm attaching my code (which worked FINE before I started mucking with it) in hopes that someone else can shed some light.

    <font face="Georgia">Sub ConcantenateRefs()
    Dim BracesString As String
    'This routine attempts to concantenate adjacent references that are in curly braces

    BracesString = "}{"

    StartHere:
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindStop
    .Text = BracesString
    .Execute FindText = BracesString
    End With

    If Selection.Find.Found = False Then
    If BracesString = "}{" Then
    BracesString = "}^w{"
    GoTo StartHere
    Else
    GoTo MacroDone
    End If
    End If

    Do While Selection.Find.Found = True
    If Not InField Then
    frmCombineRefs.Show
    Select Case frmCombineRefs.Tag
    Case "Yes"
    Selection.Delete
    Selection.InsertBefore ", "
    Selection.Collapse wdCollapseEnd
    Do While Selection.Text = " "
    Selection.Delete
    Loop
    Case "No"
    Case "Skip"
    Selection.HomeKey wdStory
    Exit Do
    Case "All"
    Selection.Delete
    Selection.InsertBefore ", "
    Selection.Collapse wdCollapseEnd
    With Selection.Find
    .Forward = True
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindStop
    With .Replacement
    .ClearFormatting
    .Text = ", "
    End With
    .Execute Replace:=wdReplaceAll, FindText:="}{^w"
    .Execute Replace:=wdReplaceAll, FindText:="}{"
    .Execute Replace:=wdReplaceAll, FindText:="}^w{^w"
    .Execute Replace:=wdReplaceAll, FindText:="}^w{"
    Exit Do
    End With
    Case Else
    End Select
    Else
    Selection.Collapse wdCollapseEnd
    End If
    Selection.Find.Execute
    Loop

    Unload frmCombineRefs

    MacroDone:
    ActiveWindow.View.ShowFieldCodes = False
    End Sub
    </font face=georgia>
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace Oddity (Word XP)

    Very strange,

    It behaves the same if you replace bracesstring with "^0125^0123} which also finds } { from the find dialog box. I will play with this a bit more to see if I can come up with another solution.

    If you are looking for fields codes then neither of these searches will help. You use ^d in the Find dialog box to find those.

    StuartR

    Edited by StuartR to add
    The culprit appears to be the use of
    <font face="Georgia">.Execute FindText = BracesString</font face=georgia>
    as well as
    <font face="Georgia">.Text = BracesString</font face=georgia>

    Replacing this with a simple
    <font face="Georgia">.Execute</font face=georgia>

    Seems to fix it.

    StuartR

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Search and Replace Oddity (Word XP)

    <P ID="edit" class=small>(Edited by jscher2000 on 25-May-04 13:28. )</P>I get good results after chopping out all the stuff that doesn't compile. This one in particular seems to be a problem:

    .Execute FindText = BracesString

    If FindText is an undeclared variant (i.e., you are not using Option Explicit), perhaps you actually are searching for the result of a boolean comparison?

    Added: I guess I should have left this one to Stuart after all. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Search and Replace Oddity (Word XP)

    In the first part you have the incorrect
    <pre> .Execute FindText = BracesString
    </pre>

    It should be
    <pre> .Execute FindText:=BracesString
    </pre>

    In the second part of the code, you got it right.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Search and Replace Oddity (Word XP)

    > I guess I should have left this one to Stuart after all

    Only beat you by about 60 seconds (edited to add - I can't even read the time on a post tonight, nearer to 600 seconds)

    StuartR

  6. #6
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Search and Replace Oddity (Word XP)

    Thanks to all for their help. I got it working in the meantime by rewriting the section, apparently sans typos. This time I got better results because (as was pointed out) the original code was using the boolean result of FindText = BracesString.
    For want of a colon...
    And I didn't mean to confuse anyone by the "If Not InField" line in my code; InField is a boolean function I wrote to skip over field codes. (It's not a baseball thing.)
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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