replace text in an opened file

  • 18 years ago

    Hi,
    Can someone help me here?  I am opening a word doc and trying to replace a text in the document.  For eg: I found the word 'hello' and I want to replace it with 'hi'.  How do I do it?
    thanks

  • 18 years ago

    Here's Word's version of a solution to your problem:


    Sub HelloHi()
    '
    ' HelloHi Macro
    ' Macro recorded 21/03/02
    '
      Selection.WholeStory
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      With Selection.Find
         .Text = "Hello"
         .Replacement.Text = "Hi"
         .Forward = True
         .Wrap = wdFindAsk
         .Format = False
         .MatchCase = False
         .MatchWholeWord = False
         .MatchWildcards = False
         .MatchSoundsLike = False
         .MatchAllWordForms = False
      End With
      Selection.Find.Execute Replace:=wdReplaceAll
    End Sub


    Incorporate this into your code and see if it helps.

  • 18 years ago

    Thank you for your help.  But, mine is getting a bit more complicated.  What if I want to find every whole word that starts with 'hello'?  For eg: How do I find "<helloJohn>" and "<helloSmith>".  All I know is that it starts with '<hello' but I need to grab the whole word '<Hello_John>'.  Is it possible to get the whole word? Can someone help?
    thank you.

  • 18 years ago

    ok.  I got it working half way.  
    I am able to find every text that starts with '<hello' and can change to '<hi'.  But, I need to grab the whole text, then replace it.   How can grab the whole text and store it?


    find:  <helloJohn>
            <hello
    William>


    replace:  <hiJo>
                  <hi
    Will>


    I can do:  <hiJohn>
                   <hi
    William>  Can not change the whole text.
    Pleeeeeeeeeeeeeeese help!

  • 18 years ago

    As you’re looking for different strings and looking to replace each one with a unique string, you’ll probably need to do a loop to search through the document for each string, replacing it, then looping through again to replace the next one, unless you can come up with a way of using the Select Case command.

  • 18 years ago

    Yes, I did try to loop through the whole document.  But what happened was, I wasn't able to grab the whole string.  I used selection.find.text = 'hello' and used a loop to replace.  But, it only replaced the word 'hello' not the whole string 'hello_John'.  Is it possible to get the whole string if I know every string that I need to replace starts with 'hello'???  Then, I can loop and replace them.
    thank you.

  • 18 years ago

    Insert this bit of code after you have found "hello".  It will extend the selection to the end of the next word.  


                 Selection.MoveEnd wdWord, 2


    You could then put the contents of the selection into a string and manipulate it using string functions.


    Hope this helps


Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan