替换Range中的多个文本

时间:2016-05-15 14:17:20

标签: vba ms-word word-vba word-2007

我想替换多个文本/标点符号,例如

带有","

" " 带有"'s"

" "

, 's是额外的文字,我下一步不需要。

Replace方法只能改变一次

有没有其他方法可以替换以下句子中的多个文字?

“Aabar仍然专注于阿布扎比的计划扩张,在2020年世博会之前的迪拜”

Replace适用于,,但's仍然存在。

Sub make_range_replace_string()
Dim R As Range
Dim F As String
Do
    selection.Find.ClearFormatting
    selection.Find.Font.Bold = True
    With selection.Find
        .Forward = True
        .Wrap = wdFindStop
    End With
    selection.Find.Execute
    If selection.Find.Found Then
   Set R = ActiveDocument.Range(selection.Range.Start, selection.Range.End)

   F = Replace(R, ",", "") 

   MsgBox F

Else
Exit Do
End If
Loop

End Sub

1 个答案:

答案 0 :(得分:2)

Doug Glancey是正确的,只需使用'而不是'虽然。 换一种说法;多次使用Replace-function。

Dim F As String

F = "Aabar remains focused on Abu Dhabi’s plans expansion, In Dubai ahead of Expo 2020"
F = Replace(F, ",", " ")
F = Replace(F, "`s", " ")
F = Replace(F, "’s", " ")
F = Replace(F, "'s", " ")
F = Replace(F, Chr(39) & "s", " ")
F = Replace(F, Chr(96) & "s", " ")

MsgBox F