PDA

View Full Version : Solved: Replace text only if not in specific style



clhare
04-08-2009, 06:35 AM
I need to search for specific text in a document and only replace the text if it is not in a specific style (ex. only replace found text if it is not in Normal DS style). Is there a way to do that? I'm stuck!

fumei
04-08-2009, 11:17 AM
Think it out.

1. search for specific text
2. test if it is the style
3. if it is not, replace it

clhare
04-08-2009, 11:18 AM
It's the part on testing if it's a specific style I'm having so much trouble with! Any hints to get me started in the right direction?

fumei
04-08-2009, 11:24 AM
If r.Style <> "stylename" Then
r.Text = strReplacementText
End If
r.Collapse Direction:=wdCollapseEnd

Collapsing the range is important.

clhare
04-08-2009, 12:13 PM
How do you assign the result of the search to a range? I tried both of the following and neither is right:

Set r = Selection.Find.Found
Set r = Selection.Range

I'm probably not even close!

fumei
04-08-2009, 01:19 PM
"How do you assign the result of the search to a range? "

A use of .Find always - ALWAYS - assigns the result of the search (a .Found) to a range.

Please post what code you are using. You are likely using Selection, which is not a good idea. But even using Selection.Find still assigns the found string to a range.

You are not stating full information, so I will make assumptions...what else can I do?

Assumptions:

1. you are searching for ONE specific string.
2. you are replacing it - if it is not Style X - with ONE specific string
3. you are searching the entire document.
Sub ReplaceIfNotStyle_X()
Dim r As Range
Dim strReplaceWith As String

strReplaceWith = "Yogi Bear"

Set r = ActiveDocument.Range
With r.Find
Do While .Execute(FindText:="BooBoo", _
Forward:=True) = True
If r.Style <> "X" Then
r.Text = strReplaceWith
r.Collapse 0
End If
Loop
End With
End Sub


Like I said...

1. search for the specific text
With r.Find
Do While .Execute(FindText:="BooBoo", _
Forward:=True) = True


2. test if it is the style
If r.Style <> "X" Then



3. if it is not, replace it
r.Text = strReplaceWith
r.Collapse 0

clhare
04-09-2009, 03:51 AM
I am trying to do exactly as you indicated... find text and if it's within a specific style leave it alone, otherwise change the text. I've done a zillion searches and replaces but I never had to check the style of the found text before and couldn't figure out how to do that.

Here's the last version I had come up with on my own, which didn't work (there were several versions and none of them worked--one version kept repeating the new text endlessly!):

Sub Test()
Dim r As Range
With ActiveDocument
' Start search at beginning of document
.HomeKey Unit:=wdStory
' Search for "ABC"
.Find.Execute FindText:="ABC", Forward:=True, Wrap:=wdFindContinue, Format:=False
' Loop while still found in document
Do While .Find.Found = True
Set r = Selection.Range
If r.Style <> "Normal DS" Then
r.Text = "DEF"
End If
r.Collapse direction:=wdCollapseEnd
' Search for next occurrence of text
.Find.Execute
Loop
' Return selection to beginning of document
.HomeKey Unit:=wdStory
End With
End Sub


Your version worked perfectly! Thank you for your help and for the extra details. I learn so much from this site!

fumei
04-09-2009, 09:01 AM
Do you understand why my version works and your version above does not? It would help you immensely if you really understood what your code is actually doing.

clhare
10-19-2012, 05:50 AM
oop! wrong thread!

macropod
10-19-2012, 03:39 PM
IMHO, unless you already have some of the replacement text in the Style you want to exclude, the simplest way is to change all the text, then swap the Find/Replace expressions and use Find/replace to restore the nominated Style's previous state.