PDA

View Full Version : Solved: Search/Replace if not in 2 particular styles



clhare
05-01-2009, 06:08 AM
I'm trying to do a search and replace for any digit followed by a space, a hyphen, another space, and any other digit and replace the 2 spaces with nonbreaking spaces and the hyphen with an en dash (so it should only find things like "27 - 31", 1 - 5", etc.) I got this to work fine until I added in the last part -- the text should only be replaced if the text is not in either of two specific styles. Once I add the part to check for the two styles, it just gets hung up. Can you help me figure out what I'm doing wrong?

With Selection.Find
.Text = "^# - ^#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
End With
Selection.Find.Execute
While Selection.Find.Found
With Selection
If .Style <> "Numbr 1-9 DS" And .Style <> "Numbr 10+ DS" Then
.MoveLeft Unit:=wdCharacter, Count:=1
.MoveRight Unit:=wdCharacter, Count:=1
.TypeText Text:=Chr(160) & Chr(150) & Chr(160)
.Delete Unit:=wdCharacter, Count:=3
.Find.Execute
End If
End With
Wend

Any help is greatly appreciated!

macropod
05-01-2009, 05:15 PM
Hi Cheryl,

Try:
Sub Test()
Dim oRng As Range, fRng As Range
Application.ScreenUpdating = False
With Selection
Set oRng = .Range
With .Find
.ClearFormatting
.Text = "^#^w^?^w^#"
.Wrap = wdFindContinue
.Forward = True
Do While .Execute = True
Set fRng = ActiveDocument.Range(Start:=Selection.Start + 1, End:=Selection.End - 1)
With fRng
If .Style <> "Numbr 1-9 DS" And .Style <> "Numbr 10+ DS" Then
If Mid(.Text, 2, 1) = Chr(45) Or Mid(.Text, 2, 1) = Chr(150) Or _
Mid(.Text, 2, 1) = Chr(151) Or Mid(.Text, 2, 1) = Chr(173) Then
.Select
Selection.TypeText Text:=Chr(160) & Chr(150) & Chr(160)
End If
End If
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With
End With
oRng.Select
Set fRng = Nothing
Set oRng = Nothing
Application.ScreenUpdating = True
End SubWith this code, any white space, hyphen, white space combination between two digits will be converted. When the macro has finished executing, the original selection will also be reinstated.

clhare
05-04-2009, 07:09 AM
Awesome!! This works beautifully! Thank you so much!

clhare
09-03-2010, 09:56 AM
The code from macropod works great, but now I'm finding that the documents this code is used in also as text boxes in the document where there could be additional instances where I need to replace text as long as it's not in one of the two stated styles.

Can anyone help me update this macro so it runs in each story in the document? I have absolutely no clue!

Thanks!

Tinbendr
09-03-2010, 12:11 PM
Take a look at this article (http://word.mvps.org/faqs/macrosvba/FindReplaceAllWithVBA.htm).

Post back if you need more help.

gmaxey
09-03-2010, 06:12 PM
Tinbendr,

This is a more comprehensive process:
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

Tinbendr
09-03-2010, 06:41 PM
Oh gee! :doh: I never noticed there were two similar articles.

Thanks for pointing that out.

gmaxey
09-03-2010, 07:44 PM
The one I pointed out was built on Doug's earlier version. I don't know how things are now, but back when the updated article was published it was easier to publish a new article than it was to edit an older one. AFAIK and as far as what testing was done the new process does dig deep (e.g., shaperanges in header/footer stories) into the document to find and replace text.