在Word 2010 VBA中,使用“查找并替换”在符号之前和之后插入空格

时间:2019-02-01 08:48:34

标签: vba ms-word

我有一个Word 2010文档,其中一些加减号用数字包围。 我想格式化加号减号,以便每个加号减号前后都有空白。

我的代码能够找到该符号。 不幸的是,ReplaceString留下了一个白框,而不是加减号。

Dim ReplaceString As String
ReplaceString = " " & ChrW(-3919) & " "

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
    .Text = ChrW(-3919) 'plus/minus symbol
    .Forward = True
    .Format = True
    .MatchCase = False
    .Replacement.Text = ReplaceString
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Replacement.ClearFormatting
    .Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

1 个答案:

答案 0 :(得分:0)

尝试:

With ActiveDocument.Range.Find
  .ClearFormatting
  .Text = ChrW(-3919) 'plus/minus symbol
  With .Replacement
    .ClearFormatting
    .Highlight = True
    .Text = " ^& "
  End With
  .Format = True
  .Forward = True
  .Wrap = wdFindContinue
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With