Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Word

Status
Not open for further replies.

bbyboo1323

Programmer
Apr 10, 2003
10
US
I am working in the immediate window of VBA with Word (2000) and I need to write a short code to find a work and replace it but when I write the code, it will not replace the word I need to replace it. Someone please help me on this! Thanks
 
Not sure exactly what you want.

Is your code simply supposed to change all occurances of one word to another word, like red to blue?

For examples you can record a macro, do a replace then edit the macro to see the code it generates.

example:
sub macro1()
' Macro1 Macro
' Macro recorded 4/10/2003 by sam
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "red"
.Replacement.Text = "blue"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
end sub
 
I need to replace one word. The word I need to replace is the only word so I could find method but none of the code works. I need to replace that word with another word. The word is you're and i have to replace it with you are.
 
can you try this?

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 4/10/2003 by sam
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "you're"
.Replacement.Text = "you are"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
Nope that didnt work either. Its in the immediate window testing so it should only be 1-3 lines long and i tried the whole code and I cant get any of it to work
 
bbyboo1323,

Why are you working in the Immediate window for a one-off process which could easily be done directly from Word or with a simple macro such as suggested by mscallisto?

If you must do it in the immediate window you need a single line of code. If you can live with the defaults you should be able to use the following. If you can't live with them you'll have to add some extra arguments.

Code:
ActiveDocument.Content.Find.Execute FindText:="you're", ReplaceWith:="you are", Replace:=wdReplaceAll


Enjoy,
Tony
 
I am still in college and for our assignment that is what we are suppose to do. Using the the immediate window to me is useless but thats what I am instructed to do. Thanks the code worked finally!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top