为什么这个VBA代码不能替换特殊字符?

时间:2011-05-17 15:01:04

标签: ms-word word-vba

我发现这个宏可以在Microsoft Word 2010中一次替换多个字符串。

我遇到了特殊字符的问题。我录制了一个宏并手动替换了这些字符,因此我可以看到如何在VBA代码中编写特殊字符。

当我尝试将它们全部放在一个字符串中时,它们就像被忽略了一样。

如何更换包括特殊字符在内的所有内容?

ChrW(728)是˘

ChrW(711)

Sub MultiReplace()
Dim StrOld As String, StrNew As String
Dim RngFind As Range, RngTxt As Range, i As Long
StrOld = "ChrW(728),ChrW(711),H"
StrNew = ".,Þ,¼"
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrOld, ","))
  Set RngFind = RngTxt.Duplicate
  With RngFind.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = Split(StrOld, ",")(i)
    .Replacement.Text = Split(StrNew, ",")(i)
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With
Next
End Sub

1 个答案:

答案 0 :(得分:2)

我明白了。替换这一行:

StrOld = "ChrW(728),ChrW(711),H"

StrOld = "" & ChrW(728) & "," & ChrW(711) & ",H"