|
|
VBA Find/Replace in Word from Excel
Hi
I'm having trouble performing a find/replace in Word 2003, from VBA in Excel. I'm essentially doing a complicated mail merge, where I open up a SourceDoc, copy that into TempDoc then do find and replace on certain strings (such as Contact) then paste that into MergeDoc. The copy and paste from SourceDoc to TempDoc works fine, however I keep getting "Method 'Execute' of object 'Find' failed."
I have tried setting all the find options first with .text and .replacement etc, but Excel just crashes in this case. My code is below (just the bit up until the find method fails.)Code: Dim WdApp As Word.Application Dim MergeDoc As Word.Document, SourceDoc As Word.Document, TempDoc As Word.Document Set WdApp = New Word.Application Set SourceDoc = WdApp.Documents.Open("C:\SourceDoc.doc", ReadOnly:=True, AddToRecentFiles:=False) Set MergeDoc = WdApp.Documents.Add Set TempDoc = WdApp.Documents.Add WdApp.Visible = True SourceDoc.Content.Copy TempDoc.Content.Paste TempDoc.Content.Find.Execute "Contact", ReplaceWith:="blah", Replace:=wdReplaceAll
If anyone could shed any light on why the find is failing that would be great, I can't see what I'm doing wrong and have tried a load of different things but can't seem to get it right.
Thanks
Andy
Never mind, after googling the exact error number (800706f7) I found this Microsoft article (kb/292744) and found that it is an error in Word. I had to change my code to use late binding instead of early, as suggested in the article.
Just thought I'd list for everyone else's reference! |
|