逐字反向文本

时间:2016-10-05 11:51:37

标签: vba text ms-word macros word-vba

我想在这样的Word文档中反转单词:" elpmaS texT"成为"示例文本"。

我试过这样的事情:

For Each word In ActiveDocument.Words
    word = StrReverse(word)
Next word

然而它并不起作用。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

当您为每个循环使用a时,您无法更改单词,因此请使用for循环:

Dim i As Integer

For i = 1 To ActiveDocument.Words.Count Step 1

    ActiveDocument.Words(i) = StrReverse(ActiveDocument.Words(i)) & " "
Next i

答案 1 :(得分:0)

Sub ReverseSelectedWords()
    Dim i As Integer
    Dim oWords As Words
    Dim oWord As Range

    Set oWords = Selection.Range.Words

    For i = 1 To oWords.Count Step 1

        Set oWord = oWords(i)

        ''Make sure the word range doesn't include a space
        Do While oWord.Characters.Last.text = " "
            Call oWord.MoveEnd(WdUnits.wdCharacter, -1)
        Loop

        Debug.Print "'" & oWord.text & "'"
        oWord.text = StrReverse(oWord.text)

    Next i

End Sub