|
-
Feb 9th, 2006, 05:33 AM
#1
Thread Starter
New Member
Find/Replace in Word application
Hi,
I've to replace some predefined text in a Word document. I''ve tried both Application.ActiveDocument.Content.Find.Execute and Application.Selection.Find.Execute but to no avail. I always get an error Method Execute of object Find failed.
Please help me out.
TIA,
Rajiv
-
Feb 9th, 2006, 05:59 AM
#2
Re: Find/Replace in Word application
Welcome to forums!
Could you post the code which you have? so that we can understand clearly to help you.
Anyway, here is the find and replace routine for you.
VB Code:
Public Function ReplaceString(sFind As String, sReplace As String, _
rngRange As Range, Optional fWildCards As Boolean = False, _
Optional fCase As Boolean = False, Optional bWholeWord As Boolean)
Application.ScreenUpdating = False
' Purpose: Function to be used for all global replaces in this template.
On Error GoTo xErrorHandler
With rngRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = fWildCards
.MatchCase = fCase
.Format = False
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll
End With
'**Clear out the Find parameters
Call ResetFind
Application.ScreenUpdating = True
Exit Function
xErrorHandler:
If Err <> 0 Then
MsgBox Err.Description, vbCritical, "ReplaceString - " & CStr(Err.Number)
End If
Application.ScreenUpdating = True
End Function
Public Function ResetFind()
Application.ScreenUpdating = False
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
End With
Application.ScreenUpdating = True
End Function
You can call this function as:
VB Code:
call ReplaceString("System", "Computer", Activedocument.Range)
This example will replace "System" with "Computer" in the active document.
I hope this will help you.
-
Feb 9th, 2006, 01:01 PM
#3
Re: Find/Replace in Word application
Whenever you need to know how to do something in most VBA apps, you can record a macro doing your needed task. Then check the module code in the VBA IDE for the generated code.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 10th, 2006, 01:35 AM
#4
Thread Starter
New Member
Re: Find/Replace in Word application
In fact i'm doing the same. Following is my code
------------------------------
VB Code:
Private Sub ReplaceInMemo(ByRef wRange As Word.Range, ByVal sFind As String, ByVal sReplace As String)
'Replace string <sFind> with <sReplace> in the template
With wRange.Find
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wRange.Find.Execute Replace:=wdReplaceAll
wRange.Find.ClearFormatting
wRange.Find.Replacement.ClearFormatting
End Sub
------------------------------
But now my application just crashes. If i run it from the IDE then the IDE itself crashes and the Error reporting message of Windows comes.
This is how i call the Sub.
ReplaceInMemo wMemo.Range, "<PACKAGE_NUMBER>", strPckName
wMemo is a Word.Document object.
Last edited by rajivonline; Feb 13th, 2006 at 06:16 AM.
-
Feb 10th, 2006, 06:29 AM
#5
Re: Find/Replace in Word application
Please post your vb code inside vb code tags. So that it will be easier to read/understand.
The code which you posted works fine for me. I haven't get any errors. Are you working in vb or word vba?
ReplaceInMemo wMemo.Range, "<PACKAGE_NUMBER>", strPckName
How you declared the wMemo? Show us the code.
It might be error on that.
-
Feb 10th, 2006, 09:43 AM
#6
Re: Find/Replace in Word application
Hm, One possibility might be your use of Find.Text. I'm not sure whether this works for Ranges or not. You may wish to put the "FindText:=" in the Execute command.
Or even move the Execute command inside the With so that it picks up all the other properties?
Worth a try.
zaza
-
Feb 22nd, 2006, 12:05 AM
#7
Thread Starter
New Member
Re: Find/Replace in Word application
Hi,
I am using the following now.
VB Code:
wRange.Find.Execute FindText:=sFind, Forward:=True, Wrap:=wdFindContinue, Format:=False, ReplaceWith:=sReplace, Replace:=wdReplaceAll
wRange.Find.ClearFormatting
wRange.Find.Replacement.ClearFormatting
But i get a run time error '-2147023113 (800706f7)' saying Method 'Execute' of object 'Find' failed.
Btw wMemo has been declared as Word.Document and i'm using VB6 SP6.
Last edited by rajivonline; Feb 22nd, 2006 at 03:22 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|