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
Helmut Weber - 04 Oct 2005 12:45 GMT
Hi,
first you have to tell us a bit more,
on how you are trying to access word.
Early binding or late binding?
Here for controlling excel form word:
http://word.mvps.org/faqs/interdev/EarlyvsLateBinding.htm
See too:
http://word.mvps.org/faqs/interdev/ControlWordFromXL.htm
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Al Borges - 06 Oct 2005 11:08 GMT
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)
> Hello all,
> I am trying to run a macro FROM EXCEL that effects the changes in a
[quoted text clipped - 37 lines]
> 'Word.Application.Quit
> End Sub
kaiser - 07 Oct 2005 04:23 GMT
Thanks guys - sorry for the delayed response - I appreciate the help