Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 2000 replace specific word macro (Word 2000)

    First, let me say I'm brand new here and a very brief look tells me I found a gold mine! It looks like I can spend hours just reading the posts! But anyway, I have a very specific request, for help with a macro I'd really, really find useful. I'm basically just a person who can record simple ones, and coding is so far not in my range. If I'm jumping here too quick, too new to just make a request, just say the word! I looked at the rules and didn't see that, though.

    I'm dying to have a macro that would find the last "the" in a sentence, and replace it with "her" or "him". For instance, if I had the sentence: She had tears streaming down the cheeks - I'd like a macro that I could assign a keystroke to and make that last "the" a "her," (She had tears streaming down her cheeks.") It may sound like a crazy request, but I use an abbreviation expander and I have millions of phrases, but most have "the" instead of "him" or "her" because an h as a short form could lead to mistakes. Anyway, I'm always going back and changing it manually, and I just thought this would be the neatest thing. Would this be possible?

    Many thanks for any replies.

    Looking forward to reading here.

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Word 2000 replace specific word macro (Word 2000)

    I think we could write a macro, but a bit more information is needed to get it right.

    Do you have a clear rule for deciding whether to use her or his or do you want to prompt the user to decide for each paragraph?
    Do you want to process every paragraph, or is there a rule to identify which ones to skip?

    StuartR

  3. #3
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word 2000 replace specific word macro (Word 2000)

    Thank you very much, Stuart! That would be fantastic, really fantastic!

    I would definitely want a prompt for him or her, or I guess, if the macro is the same for each one but just a matter of putting "him" or "her" into the macro at the right place, I could simply copy it and name them something different so that I have 2 of them, and assign a different keystroke to each. Either one of those, whatever is the most practical. Speed is of the essence for me, that's the main thing I think about.

    I may not need it for every paragraph; there isn't any way for me to know when I'll need it or how often in a report. I hope I'm understanding your question correctly. I just would call it up as it's needed.

    Many thanks!

    PS: Editing to say: you may mean rules for finding which "the" in the paragraph, and I would just want it to look for the last one. It's something I'd do pretty quickly after finishing the sentence.

  4. #4
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Important - I meant HIS, not HIM

    I'm very sorry! Not a good way to start my very first posts, is it! My apologies, but I need a "her" or a "his" - why I kept saying him I don't know.

    Sorry and thank you.

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Important - I meant HIS, not HIM

    Here is one of your Macros, it will replace the last "the " in every paragraph in the current selection with "her ". You can copy this, replace her with his and change the name for the other one.

    StuartR
    <font face="Georgia">
    Public Sub TheToHer()
    Dim paraNext As Paragraph
    Dim i As Integer

    For Each paraNext In Selection.Paragraphs
    For i = paraNext.Range.Words.Count To 1 Step -1
    If paraNext.Range.Words(i).Text = "the " Then
    paraNext.Range.Words(i).Text = "her "
    Exit Sub
    End If
    Next i
    Next paraNext

    End Sub
    </font face=georgia>

  6. #6
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Important - I meant HIS, not HIM

    OMG, this is very, very, very much appreciated! Thank you, Stuart! I've put it in and I'm getting an error that says "expected end sub", but I think I can probably figure out what I'm doing wrong. If not, I'll post back. This is really neat, especially since I've realized there are other ways I can use this with just a little manipulation. Thank you again!

  7. #7
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Important - I meant HIS, not HIM

    Just wanted to say I got it; didn't realize at first about the public sub thing.

    It works like a dream! Thumbs up to you, Stuart!

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Important - I meant HIS, not HIM

    I just noticed a mistake in the code, but I don't have time to fix it before I go to work this morning. It will do the right thing for a single paragraph, but if you select multiple paragraphs and run it then only the first one will be changed.

    StuartR

  9. #9
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Important - I meant HIS, not HIM

    Hi. In my own case, I can't see myself ever selecting multiple paragraphs, only single, so it's fine for me. I also made a copy to have it replace "states" with "says," another one I can really use, so it's going to benefit me highly! I will of course replace it if you change the code; maybe I should think on the multiple paragraph situation!

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Important - I meant HIS, not HIM

    Here is a fixed version of the code. I have broken it into two macros. The public one just calls the other one once for each paragraph.

    <font face="Georgia">

    Public Sub HimHer()
    Dim paraNext As Paragraph
    For Each paraNext In Selection.Paragraphs
    ReplaceLastWord paraNext.Range, "him ", "her "
    Next paraNext
    End Sub

    Private Sub ReplaceLastWord(rngTarget As Range, strOld As String, strNew As String)
    Dim i As Integer
    For i = rngTarget.Words.Count To 1 Step -1
    If rngTarget.Words(i).Text = strOld Then
    rngTarget.Words(i).Text = strNew
    Exit Sub
    End If
    Next i
    End Sub
    </font face=georgia>

    You can use the same private macro called with different strOld and strNew parameters, so you could add (for example)

    Public Sub TheHis()
    Dim paraNext As Paragraph
    For Each paraNext In Selection.Paragraphs
    ReplaceLastWord paraNext.Range, "the ", "his "
    Next paraNext
    End Sub

    StuartR

  11. #11
    New Lounger
    Join Date
    Feb 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Important - I meant HIS, not HIM

    That's great, Stuart. Thank you very much. This certainly was a great introduction to this board, I must say!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •