1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Problem with code in Word

Discussion in 'Business Applications' started by caveatashish, May 7, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. caveatashish

    caveatashish Thread Starter

    Joined:
    May 7, 2008
    Messages:
    4
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = True
    .Execute FindText:="Inc aa", _
    ReplaceWith:="incorporated", Replace:=wdReplaceAll
    End With

    if my document contains below line of code


    inc aa for the inc aabcde and the inc aa

    the above code will replace inc aabcde also

    is this a bug in ms word
     
  2. slurpee55

    slurpee55

    Joined:
    Oct 20, 2004
    Messages:
    7,837
    a minor change will fix it:
    Code:
    With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchWholeWord = True
    .Execute FindText:="Inc", _
    ReplaceWith:="incorporated", replace:=wdReplaceAll
    End With
     
  3. caveatashish

    caveatashish Thread Starter

    Joined:
    May 7, 2008
    Messages:
    4
    Hi

    Your code is almost same as my code is.
     
  4. JohnWilson

    JohnWilson

    Joined:
    Nov 22, 2007
    Messages:
    262
    it's not! You are trying to match a whole word with two words! Do two replaces one for inc and one for aa
     
  5. slurpee55

    slurpee55

    Joined:
    Oct 20, 2004
    Messages:
    7,837
    All I did was take yours and get rid of the space and the aa.
    If you only want to replace words that have "inc aa" (as opposed to, say, "inc oa") then change the line in your code to
    ReplaceWith:="incorporated aa", replace:=wdReplaceAll
     
  6. caveatashish

    caveatashish Thread Starter

    Joined:
    May 7, 2008
    Messages:
    4
    Hi john

    if i need to replace "bed sheet" with "cot cloth" and if i use replace two time for bed->cot and sheet->cloth
    and my document have the below text

    "bed sheet over the table where paper sheet was on the wooden bed"
    imagine the output if i use above code
    the red color word will also get changed
     
  7. slurpee55

    slurpee55

    Joined:
    Oct 20, 2004
    Messages:
    7,837
    Yes, that is why you would replace "inc aa" with "incorporated aa" - it takes the first text and replaces it with the second. replacing "inc aa" with "incorporated" would change "inc aa1234" into "incorporated1234"
     
  8. caveatashish

    caveatashish Thread Starter

    Joined:
    May 7, 2008
    Messages:
    4
    Hi Slurpee

    Is there any way to over come such problem in word because i m developing an addins for ms word where i have a replace function.
     
  9. slurpee55

    slurpee55

    Joined:
    Oct 20, 2004
    Messages:
    7,837
    Well, you have to be very specific in what you want replaced and what you want replacing it. If you are trying to just replace all cases where you have "inc" typed in rather than having to type out "incorporated" you will have problems, for "inc" appears as part of so many other words (incredible, incredulous, etc.).
    If it is always "Inc" that will help you, but it still would not stop an error where, say, a sentence began "Incredibly,...."
    So I need to know a good bit more about what exactly you are trying to accomplish.
     
  10. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Just add a space before the second quoation mark.

    Use .Execute FindText:="Inc aa " instead of .Execute FindText:="Inc aa"


    Regards,
    Rollin
     
  11. slurpee55

    slurpee55

    Joined:
    Oct 20, 2004
    Messages:
    7,837
    Hi Rollin, I wasn't sure if "aa" was part of what he wanted to replace or if it is just an indicator of those "inc"s that need replacing or...?
     
  12. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    I guess we need for the OP to explain a little better exactly what he is trying to accomplish. Caveat, can you post a sample document with some real data so we better understand the whole process?

    Regards,
    Rollin
     
  13. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/710407

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice