Sub 倒序文字() ' 2017/7/12 將選取文字次序倒轉;若無選取,則處理整份文件內容
Dim wCount As Long, oRange As Range, i As Long, j As Long, dChars As Characters
'Set d = ActiveDocument
If Selection.Type = wdNoSelection Or Selection.Type = wdSelectionIP Then '如果沒有選取則處理整個文件
Set oRange = d.Content: Set dChars = oRange.Characters: wCount = oRange.Characters.Count
If wCount = Len(oRange.Text) Then '如果沒有2個字元長的漢字
d.Range = oRange + VBA.StrReverse(oRange)
Else
d.Range.InsertParagraphAfter
GoSub forChar
End If
Else '如果有選取範圍,則只處理選取的內容
Set oRange = Selection.Range: Set dChars = oRange.Characters: wCount = oRange.Characters.Count
If wCount = Len(oRange.Text) Then '如果沒有2個字元長的漢字
d.Range = d.Range + VBA.StrReverse(oRange)
Else
wCount = wCount + 1
d.Range.Paragraphs.Add
GoSub forChar
End If
End If
Exit Sub
forChar:
Selection.EndKey wdStory
For i = wCount - 1 To 1 Step -1
j = j + 1
Selection.Font.Name = oRange.Characters(wCount - j).Font.Name
Selection.Font.Size = oRange.Characters(wCount - j).Font.Size
Selection.Font.NameFarEast = oRange.Characters(wCount - j).Font.NameFarEast
Selection.Font.ColorIndex = wdDarkRed
Selection.TypeText oRange.Characters(wCount - j)
Next i
Exit Sub
Return
End Sub