Results 1 to 7 of 7

Thread: Find/Replace in Word application

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Location
    India
    Posts
    5

    Post 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

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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:
    1. Public Function ReplaceString(sFind As String, sReplace As String, _
    2.                             rngRange As Range, Optional fWildCards As Boolean = False, _
    3.                             Optional fCase As Boolean = False, Optional bWholeWord As Boolean)
    4. Application.ScreenUpdating = False
    5. ' Purpose: Function to be used for all global replaces in this template.
    6.     On Error GoTo xErrorHandler
    7.     With rngRange.Find
    8.         .ClearFormatting
    9.         .Replacement.ClearFormatting
    10.         .Text = sFind
    11.         .Replacement.Text = sReplace
    12.         .Forward = True
    13.         .Wrap = wdFindStop
    14.         .MatchWildcards = fWildCards
    15.         .MatchCase = fCase
    16.         .Format = False
    17.         .MatchWholeWord = False
    18.         .Execute Replace:=wdReplaceAll
    19.     End With
    20.     '**Clear out the Find parameters
    21.     Call ResetFind
    22.     Application.ScreenUpdating = True
    23.     Exit Function
    24.  
    25. xErrorHandler:
    26.     If Err <> 0 Then
    27.         MsgBox Err.Description, vbCritical, "ReplaceString - " & CStr(Err.Number)
    28.     End If
    29.     Application.ScreenUpdating = True
    30. End Function
    31.  
    32. Public Function ResetFind()
    33. Application.ScreenUpdating = False
    34. With Selection.Find
    35.     .ClearFormatting
    36.     .Replacement.ClearFormatting
    37.     .Text = ""
    38.     .Replacement.Text = ""
    39.     .Forward = True
    40.     .Wrap = wdFindContinue
    41.     .Format = False
    42.     .MatchCase = False
    43.     .MatchWholeWord = False
    44.     .MatchWildcards = False
    45.     .MatchSoundsLike = False
    46.     .MatchAllWordForms = False
    47. End With
    48. Application.ScreenUpdating = True
    49. End Function
    You can call this function as:
    VB Code:
    1. call ReplaceString("System", "Computer", Activedocument.Range)
    This example will replace "System" with "Computer" in the active document.
    I hope this will help you.
    CS

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Location
    India
    Posts
    5

    Post Re: Find/Replace in Word application

    In fact i'm doing the same. Following is my code


    ------------------------------
    VB Code:
    1. Private Sub ReplaceInMemo(ByRef wRange As Word.Range, ByVal sFind As String, ByVal sReplace As String)
    2.     'Replace string <sFind> with <sReplace> in the template
    3.    
    4.     With wRange.Find
    5.         .Text = sFind
    6.         .Replacement.Text = sReplace
    7.         .Forward = True
    8.         .Wrap = wdFindContinue
    9.         .Format = False
    10.         .MatchCase = False
    11.         .MatchWholeWord = False
    12.         .MatchWildcards = False
    13.         .MatchSoundsLike = False
    14.         .MatchAllWordForms = False
    15.     End With
    16.     wRange.Find.Execute Replace:=wdReplaceAll
    17.        
    18.     wRange.Find.ClearFormatting
    19.     wRange.Find.Replacement.ClearFormatting
    20. 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.

  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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.
    CS

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Location
    India
    Posts
    5

    Unhappy Re: Find/Replace in Word application

    Hi,

    I am using the following now.

    VB Code:
    1. wRange.Find.Execute FindText:=sFind, Forward:=True, Wrap:=wdFindContinue, Format:=False, ReplaceWith:=sReplace, Replace:=wdReplaceAll
    2.  
    3.     wRange.Find.ClearFormatting
    4.     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
  •  



Click Here to Expand Forum to Full Width