View Full Version : Solved: Find and Replace Sub
fredlo2010
04-28-2012, 07:26 PM
Hello.
I am trying to work on this long document that requires several find and replace actions. So I am trying to do is to create a sub with the Text1 and Text2 as variables and then create another subroutine and feed those elements
This is want I have until now, but it does not work
The Sub
Sub ReplaceStringWithAnother(text1 As String, text2 As String)
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Content
rngSearch.Find.ClearFormatting
rngSearch.Find.Replacement.ClearFormatting
With rngSearch.Find
.Text = "text1"
.Replacement.Text = "tex2"
.Wrap = wdFindContinue
End With
rngSearch.Find.Execute Replace:=wdReplaceAll
End Sub
The sub that will feed the variables
ReplaceStringWithAnother "USD", ""
Thanks so much for the help.
:hi:
fumei
04-28-2012, 07:45 PM
"This is want I have until now, but it does not work"
Will you please stop doing that? "does not work" tells us NOTHING.
I can make a guess but...
Nothing happens at all?
It works once, but not afterwords?
It works, but loops forever?
Tell us what IS happening.
BTW: I hope this is a typo .Replacement.Text = "tex2"as it is supposed to be text2.
fredlo2010
04-28-2012, 08:03 PM
Nothing happens at all,
yes it is a typo and still doesn't do anything after fixed
macropod
04-28-2012, 09:39 PM
Perhaps you should look at '.Text = "text1"' too.
Teeroy
04-28-2012, 10:09 PM
I think Macropod's hint is on the money but may be a bit too cryptic :thumb
By enclosing the variable name in quotes you're actually feeding a string containing the variable name rather than the variable contents. It goes without saying that this is the same for both text1 and text2.
fredlo2010
04-28-2012, 10:50 PM
Thanks for you help,
Yes the problem was the quotation marks. I just found out reading in another forum. It was right in front of my nose and I couldn't see it. I am trying to apply what I have learned in the forum and my book and use it to solve to new problems; but at the end hands on is what makes the trick.
I will try to be more specific when describing my problems as well.
Here is my final code.
Public Sub ReplaceTextWithText(text1 As String, text2 As String)
Dim FindObject As Word.Find
Set rngSearch = ActiveDocument.Content
With rngSearch.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = text1
.Replacement.Text = text2
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub
By the way guys. How do I mark this thread as solved? I have looked every where and I cannot find it. A screen shoot will do the job I guess. Its no under "Thread Tools" :think:
gmaxey
04-30-2012, 04:33 AM
Hello.
I am trying to work on this long document that requires several find and replace actions. So I am trying to do is to create a sub with the Text1 and Text2 as variables and then create another subroutine and feed those elements
This is want I have until now, but it does not work
The Sub
Sub ReplaceStringWithAnother(text1 As String, text2 As String)
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Content
rngSearch.Find.ClearFormatting
rngSearch.Find.Replacement.ClearFormatting
With rngSearch.Find
.Text = "text1"
.Replacement.Text = "tex2"
.Wrap = wdFindContinue
End With
rngSearch.Find.Execute Replace:=wdReplaceAll
End Sub
The sub that will feed the variables
ReplaceStringWithAnother "USD", ""
Thanks so much for the help.
:hi:
You can use my add-in: http://gregmaxey.mvps.org/word_tip_pages/vba_find_and_replace.html
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.