PDA

View Full Version : Reverse String (preserve formatting)



gmaxey
01-16-2014, 03:29 PM
In another forum, I was asked how to reverse a selected string and preserve formatting.

E.g.,

This is a test

tset a si sihT

After much grief, I worked it out as follows. I can't understand why I had to move the range on the first pass of the loop? Probably something to do with the Selection object which always gives me fits. Thanks.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngBefore As Word.Range
Dim i As Long
Dim bMove As Boolean
Set oRng = Selection.Range.Duplicate
Set oRngBefore = oRng.Duplicate
oRngBefore.Collapse wdCollapseStart
bMove = True
For i = 1 To oRng.Characters.Count
oRng.Characters(i).Copy
oRngBefore.Paste
If bMove Then
oRng.MoveStart wdCharacter, 1
bMove = False
End If
oRngBefore.Collapse wdCollapseStart
Next
oRng.Delete
End Sub