Newbie trying to search and replace word in word doc from excel

K

kaiser

Hello all,

I am trying to search a word doc and change all words "test" with
"done", all from an excel macro.

I know there are shorter ways of eliminating a lot of text in my macro
(with , end with etc) but for my learning purposes now I would like to
avoid them.

Here is my code THAT DOESNT work.....any comments on what I am doing
wrong pls -the error message is "Method or data member cannot be found
when I try run it on the ".Replacement.Text = "Done"" line

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.Replacement
.Text = "test"
.Replacement.Text = "Done"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Word.Application.ActiveDocument.Range.Find.Execute
Replace:=wdReplaceAll

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

Dave Peterson

It looks like you have an extra .Replacement in this line.
Word.Application.ActiveDocument.Range.Find.Replacement
 
K

kaiser

thanks dave - have corrected it to the below - except now it doesnt
make any changes (and the word test exists in the document)

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
Word.Application.ActiveDocument.Range.Find.Execute
Replace:=wdReplaceAll

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

Dave Peterson

It sure looks like it should work.

How is word created?

do you use:
dim word as object
set word = createobject("word.application")

or do you have a reference set to Microsoft word?
dim Word as word.application
set word = new word.application


If you use createobject version, maybe it's because "wdFindContinue" and
"wdReplaceAll" are known to excel?

try:
..Wrap = 1 'wdFindContinue

and
Replace:=2 'wdReplaceAll

In word, you can open it's VBE, hit ctrl-g to see the immediate window and
?wdFindcontinue

to find what these contants are.
 
D

Dave Peterson

I don't have another guess. The code kept crashing excel for me.

You may want to post in a Word newsgroup if no one jumps in to help.
 

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