Results 1 to 5 of 5

Thread: microsoft word reverse text string or words order

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Try:

    Sub ReverseWords()
    Dim strIn As Variant, strOut As String, lngIndex As Long
      'Get the selected text, split by blanks into words
      strIn = Split(Selection)
      'Construct the output string
      For lngIndex = 0 To UBound(strIn)
        strOut = strIn(lngIndex) & " " & strOut
      Next
      'Replace the selected text
      Selection = Trim(strOut)
    lbl_Exit:
      Exit Sub
    End Sub
     
    Sub TransposeText()
    Dim oRng As Word.Range
    Dim lngCase As Long
    Dim strText As String
    Dim lngIndex As Long
    Dim bSpace As Boolean
      With Selection
        If .Words.Count = 1 Then
          Set oRng = .Range
          lngCase = oRng.Words(1).Case
          strText = StrReverse(oRng.Text)
          oRng.Text = strText
          oRng.Case = lngCase
        Else
          Set oRng = .Range
          For lngIndex = 1 To oRng.Words.Count
            If oRng.Words(lngIndex).Characters.Last = Chr(32) Then bSpace = True
            lngCase = oRng.Words(lngIndex).Case
            strText = StrReverse(Trim(oRng.Words(lngIndex).Text))
            If bSpace Then strText = strText & " "
            oRng.Words(lngIndex).Text = strText
            bSpace = False
          Next
        End If
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    See:http://gregmaxey.mvps.org/word_tip_p...ng_macros.html forinstructions to employ the VBA code provided above.
    Last edited by gmaxey; 05-25-2015 at 07:29 PM. Reason: Change variable type
    Greg

    Visit my website: http://gregmaxey.com

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
  •