【问题标题】:Reverse text word by word逐字反转文本
【发布时间】:2016-10-05 11:51:36
【问题描述】:

我想像这样反转 Word 文档中的单词:“elpmaS texT”变成“示例文本”。

我尝试过这样的事情:

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

但是它不起作用。

我该怎么做?

【问题讨论】:

    标签: vba text macros ms-word


    【解决方案1】:

    当你使用 for each 循环时,你无法改变单词,所以使用 for 循环:

    Dim i As Integer
    
    For i = 1 To ActiveDocument.Words.Count Step 1
    
        ActiveDocument.Words(i) = StrReverse(ActiveDocument.Words(i)) & " "
    Next i
    

    【讨论】:

    • 谢谢。如何对选定的文本执行此操作?
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 2016-03-03
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多