PDA

View Full Version : [SOLVED:] Reverse text for each paragraph



Ethen5155
06-29-2017, 07:09 AM
Hi,

well i'm looking for a way to reverse text strings in big word file but for each paragraph not for all text at one same time

i found this macro below but it reverses all text in one time

for ex:

this text




A truck is on a motorway in China the truck has millions of bees on it.

The bees fly everywhere. The police close the motorway.

The beehives are all over the motorway.


turns into



motorway. the over all are beehives motorway.

The the close police The everywhere. fly bees it.

The on bees of millions has truck the China in motorway a on is truck A





but i want it to be like:





it. on bees of millions has truck the China in motorway a on is truck A

motorway. the close police The everywhere. fly bees The

motorway. the over all are beehives The




like to be read from right to left instead of reading left to right





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






and Thanks in advance

gmaxey
06-29-2017, 03:45 PM
Sub ReverseTextByParagraph()
Dim oPar As Paragraph
Dim oRng As Range
For Each oPar In ActiveDocument.Range.Paragraphs
Set oRng = oPar.Range
oRng.End = oRng.End - 1
oRng.Text = fcnReverseString(oRng.Text)
Next
End Sub

Function fcnReverseString(strIn As String) As String
Dim arrIn() As String, strOut As String, lngIndex As Long
'Split input by blanks into words
arrIn = Split(strIn)
'Construct the output string
For lngIndex = 0 To UBound(arrIn)
strOut = arrIn(lngIndex) & " " & strOut
Next
fcnReverseString = Trim(strOut)
End Function

Ethen5155
06-30-2017, 06:39 AM
Hi Greg,

Thanks bro for your efforts, works well :) :)

Best Regards