Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Feb 2004
    Location
    Washington, District Of Columbia, USA
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Macro help, please? Switch period to comma, or vice versa.

    A surprising amount of the work I edit has one particular quirky error. Either run-on sentences with commas where periods would have been better, or choppy fragments where the periods ought to be commas.

    I'd love to have a Word 2010 macro that lets me drop my insertion point next to a comma or a period and invoke the macro. The macro would find the current mark and then replace it with the other.

    It would be extra nice if the macro didn't mind whether the IP was immediately before, or after, the offending mark. It would in either case find the adjacent mark and then switch it with the other mark.

    Does anyone know how to help me? ("Help me" as in write the macro for me !!)
    Doug

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The following macro will swap periods & commas for any selected range:
    Code:
    Sub RePunctuate()
    Dim i As Long
    With Selection
      For i = 1 To .Words.Count
        With .Words(i)
          If .Characters.First = "." Then
            .Characters.First = ","
          ElseIf .Characters.First = "," Then
            .Characters.First = "."
          End If
        End With
      Next
    End With
    End Sub
    Note: The macro only looks at what is selected; if the selection is just the IP and that is before, or after, the offending mark, it won't be processed.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    Charles Kenyon (2015-11-22)

  4. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Macropod's code goes very close to doing what you asked. It actually can work with only an insertion point but not if it is positioned at the end of the paragraph which is where I would expect you are trying to run this macro most often.

    The following should be more tolerant of where the insertion point is positioned but likely will give you bad results if you are actually making a selection which includes both commas and full stops.
    Code:
    Sub RePunctuate2()
      Dim i As Long, aRng As Range
      With Selection
        Set aRng = ActiveDocument.Range(Start:=.Range.Start, End:=.Range.End)
      End With
      If aRng.Start > 0 Then aRng.Start = aRng.Start - 1
      If aRng.End < ActiveDocument.Range.End Then aRng.End = aRng.End + 1
      If InStr(aRng.Text, ",") > 0 Then
        aRng.Text = Replace(aRng.Text, ",", ".")
      Else
        aRng.Text = Replace(aRng.Text, ".", ",")
      End If
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    That gives me an idea. The code I posted can be made to work with the insertion point by adding:
    Code:
      If .Type = wdSelectionIP Then
        If .Start <> ActiveDocument.Range.Start Then .Start = .Start - 1
        If .Start <> ActiveDocument.Range.End Then .End = .End + 1
      End If
    after 'With Selection'
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #5
    New Lounger
    Join Date
    Feb 2004
    Location
    Washington, District Of Columbia, USA
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Macropod and Andrew -- thanks very much! Doug

Tags for this Thread

Posting Permissions

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