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

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Macro help, please? Switch period to comma, or vice versa.

    Author
    Topic
    #503278

    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

    Viewing 2 reply threads
    Author
    Replies
    • #1538767

      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
      [Fmr MS MVP - Word]

    • #1538797

      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  0 Then
          aRng.Text = Replace(aRng.Text, “,”, “.”)
        Else
          aRng.Text = Replace(aRng.Text, “.”, “,”)
        End If
      End Sub
    • #1538825

      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
      [Fmr MS MVP - Word]

      • #1538847

        Macropod and Andrew — thanks very much! Doug

    Viewing 2 reply threads
    Reply To: Macro help, please? Switch period to comma, or vice versa.

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: