PDA

View Full Version : Error in Search/Replace code



clhare
05-11-2009, 12:21 PM
I have the following code that will search the document for specific text and replace only the first occurrence found that is not in either of 2 specific styles. (If the first occurrence is in one of the two specified files, the macro needs to ignore the text and look for the next occurrence and check the styles.)

When I was testing the macro it worked fine. Now, when I run it on a different file, I get an error that says "Object Variable or With Block not Set" message and I can't figure out what's wrong with it. When I debug the macro, it stops on the bolded line below.


Sub DoFindReplace_FirstOccurrence(strFindText As String, strReplaceText As String)
' Declare variables
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
r.Find.Execute
If r.Style <> "Numbr 1-9 DS" And r.Style <> "Numbr 10+ DS" Then
r.Text = strReplaceText
End If
End Sub


Any help is greatly appreciated!

lucas
05-11-2009, 12:48 PM
Does the style exist in the new document?

clhare
05-12-2009, 05:02 AM
Yes, the styles do both existing in the document.

joms
05-12-2009, 07:04 AM
hi try this, hope it helps.. :)


Sub DoFindReplace_FirstOccurrence(strFindText As String, strReplaceText As String)
' Declare variables
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
r.Find.Execute

With r

If r.Style <> "Numbr 1-9 DS" And r.Style <> "Numbr 10+ DS" Then
r.Text = strReplaceText
End If

End With

End Sub

clhare
05-12-2009, 07:16 AM
I still get an error on that line that lists the two styles. I just don't get it!

lucas
05-12-2009, 07:18 AM
can you post an example document without any private info....

macropod
05-13-2009, 06:54 PM
Hi Cheryl,

Try:
Sub DoFindReplace_FirstOccurrence(strFindText As String, strReplaceText As String)
Application.ScreenUpdating = False
With Selection
With .Find
.ClearFormatting
.Text = strFindText
.Wrap = wdFindContinue
.Forward = True
Do While .Execute = True
With Selection
If .Style = "Numbr 1-9 DS" Or .Style = "Numbr 10+ DS" Then
.Collapse Direction:=wdCollapseEnd
Else
.TypeText Text:=strReplaceText
End
End If
End With
Loop
End With
End With
Application.ScreenUpdating = True
End Sub

clhare
05-14-2009, 06:46 AM
Sub DoFindReplace_FirstOccurrence(strFindText As String, strReplaceText As String)
' Declare variables
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
r.Find.Execute

With r
If .Find.Found = True Then
If r.Style <> "Numbr 1-9 DS" And r.Style <> "Numbr 10+ DS" Then
r.Text = strReplaceText
End If

End With

End Sub

clhare
05-14-2009, 06:52 AM
Oops! I don't know what I did there! Somehow what I was working on posted before I was done with it!

Here's the macro I ended up using. I had to add to it to only replace if the text was found. Otherwise, if there were no occurrences of the text, it changed all the text to the replacement string.

Sub DoFindReplace_FirstOccurrence(strFindText As String, strReplaceText As String)
' Declare variables
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
End With
r.Find.Execute

With r
' Only continue if an occurrence has been found
If .Find.Found = True Then
If r.Style <> "Numbr 1-9 DS" And r.Style <> "Numbr 10+ DS" Then
r.Text = strReplaceText
End If
End If
End With
End Sub