Newbie question about find and replace

K

kaiser

Hello all,
I am trying to run a macro FROM EXCEL that effects the changes in a
WORD doc. Ie : I want to use the word Find and Replace but call it
from excel...so when I runthe program in excel it makes the changes in
word. here is the code that I gained from the Macro Recorder in
word...it runs fine but doesnt actually change any fields

any advice?

Sub AddData()


Dim StringToSearch As String
Dim counter As Long


'StringToSearch = "test"


'Word.Application.WindowState = wdWindowStateMaximize
Word.Application.Documents.Open ("c:\Temp\destination.doc")


'Word.Application.ActiveDocument.Range.InsertAfter
'StringToSearch


Word.Application.ActiveDocument.Range.Find.Replacement.ClearFormatting
With Word.Application.ActiveDocument.Range.Find
.Text = "test"
.Replacement.Text = "Done"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Selection.Find.Execute Replace:=wdReplaceAll
Word.Application.Selection.Find.Execute Replace:=wdReplaceAll

'Word.Application.ActiveDocument.Save
'Word.Application.Quit
End Sub
 
A

Al Borges

Hi there:

Seems to me that you need to change your code-

FROM:

Word.Application.ActiveDocument.Range.Find.Replacement.ClearFormatting
With Word.Application.ActiveDocument.Range.Find
.Text = "test"
.Replacement.Text = "Done"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Selection.Find.Execute Replace:=wdReplaceAll
Word.Application.Selection.Find.Execute Replace:=wdReplaceAll

TO:

' get rid of statement
"Word.Application.ActiveDocument.Range.Find.Replacement.ClearFormatting"
With Word.ActiveDocument.Content.Find
.ClearFormatting
.Text = "test"
.Replacement.Text = "Done"
.Execute Replace:=wdReplaceAll
End With
Call Resetsearch()
..... YOUR OTHER CODE TO FINISH THE SUB
End Sub

Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

That way when the With ... End With executes, the .Execute Replace..
statement fires with each instance of .Text = "test". By being able to call
the "Resetsearch", it makes for less coding if you are replacing numerous
text finds. Makes sense to me- see if this works for you.

Regards,
Al
(writer of the free "MS Word EMR Project", downloadable at
http://www.emrupdate.com/freestuff/alborgesmd.aspx)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top