Results 1 to 10 of 10
  1. #1
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need some cleverness in Word Macro (2003)

    I'm trying to create a macro that will abbreviate month names in certain circumstances. Right now, I've got a long list of search-and-replace definitions, such as finding September 1 and replacing it with Sept. 1.

    I want to search for a month name in a document, then determine whether one or two numbers follow (I've actually gotten it to work this far), but then if it matches, I need to convert the month name to a specified abbreviation. (The Word default for three letters won't work for September, which needs to be Sept. instead of Sep). When I do this with the search-and-replace string, I lose the date number. In WordBasic, I could create a CASE definition, but I don't know how this is done in VBA.

    If anyone can point me in the right direction, I would be most grateful.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need some cleverness in Word Macro (2003)

    Does this do what you want?

    Sub ReplaceMonths()
    ReplaceMonth "January", "Jan."
    ReplaceMonth "February", "Feb."
    ReplaceMonth "March", "Mar."
    ReplaceMonth "April", "Apr."
    ReplaceMonth "June", "Jun."
    ReplaceMonth "July", "Jul."
    ReplaceMonth "August", "Aug."
    ReplaceMonth "September", "Sept."
    ReplaceMonth "October", "Oct."
    ReplaceMonth "November", "Nov."
    ReplaceMonth "December", "Dec."
    End Sub

    Sub ReplaceMonth(strMonth As String, strAbbreviation As String)
    Dim rng As Range
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Text = strMonth
    .MatchWholeWord = True
    Do While .Execute
    Set rng = Selection.Range
    rng.MoveEnd Unit:=wdWord, Count:=2
    If IsNumeric(rng.Words(2)) Then
    Selection.Text = strAbbreviation
    Selection.Collapse wdCollapseEnd
    End If
    Loop
    End With
    End Sub

  3. #3
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need some cleverness in Word Macro (2003)

    YES!! And ... no. It does what I want when the month is followed by a number, and that's great! I am thrilled with what you've provided, and will definitely use it.

    However, I need it to NOT change the month name when the number is a four-digit year. This changed September 2005 to Sept. 2005. I can follow this with a clean-up search-and-replace macro to change Sept. 2005 back to September 2005, but if there's a way to accommodate that in this macro, I'd love it.

    You're awesome, Hans. What do you require? My first-born? ;-)

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need some cleverness in Word Macro (2003)

    Umm, just thanks is enough <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    You can replace the test

    If IsNumeric(rng.Words(2)) Then

    with

    If IsNumeric(rng.Words(2)) And Len(rng.Words(2)) < 4 Then

  5. #5
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need some cleverness in Word Macro (2003)

    That did it! You're the best!

  6. #6
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need some cleverness in Word Macro (2003)

    While I still think you're the best, I can say that I'm not ... I've been trying to run the macro again, and now I'm getting a Run-Time error. It worked fine earlier, and now it tells me the Replace With text contains a group number which is out of range.

    Why would it work fine (several times!) and then suddenly stop working? What did I do wrong??

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need some cleverness in Word Macro (2003)

    The code I posted doesn't use the Replace With text. Below the line

    <code>.Text = strMonth</code>

    insert a new line

    <code>.Replacement.Text = ""</code>

  8. #8
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need some cleverness in Word Macro (2003)

    Hi there, me again. :-)

    The macro thus far has been marvelous, and works exactly as I needed it to ... the problem is that now I've encountered some rogue writers who insist on including the ordinal after the date -- for example, September 21st or November 3rd. When this happens, the macro doesn't "read" the date and doesn't abbreviate the month. I would love to be able to strip the ordinal from the date. Any suggestions?

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need some cleverness in Word Macro (2003)

    Try this version:

    Sub ReplaceMonth(strMonth As String, strAbbreviation As String)
    Dim rng As Range
    Dim strWord As String
    Dim intVal As Integer
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Text = strMonth
    .Replacement.Text = ""
    .MatchWholeWord = True
    Do While .Execute
    Set rng = Selection.Range
    rng.MoveEnd Unit:=wdWord, Count:=2
    strWord = rng.Words(2)
    intVal = Val(strWord)
    If intVal > 0 And intVal < 32 Then
    strWord = Replace(strWord, "st", "")
    strWord = Replace(strWord, "nd", "")
    strWord = Replace(strWord, "rd", "")
    strWord = Replace(strWord, "th", "")
    rng.Text = strAbbreviation & " " & strWord
    End If
    Loop
    End With
    End Sub

  10. #10
    Lounger
    Join Date
    Apr 2003
    Location
    Salt Lake City, Utah, USA
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need some cleverness in Word Macro (2003)

    Breathtaking!

    Thanks! :-D

Posting Permissions

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