使用“宏”删除WORD中的重复字

2025-11-06 04:49:11

有如下文档,要求删除里面的重复字符。

使用“宏”删除WORD中的重复字

一、使用“开发工具”创建“宏”

使用“宏”删除WORD中的重复字

二、建立宏。主要代码如下

Dim str As String

Dim bstr As String

Dim cf As String

Dim i As Long

Selection.WholeStory

str = Selection.Text

 For i = 1 To Len(str)

    Selection.Find.ClearFormatting

    Selection.Find.Replacement.ClearFormatting

    With Selection.Find

        .Text = Mid(str, i, 1)

        .Replacement.Text = ""

        .Forward = True

        .Wrap = wdFindContinue

        .Format = False

        .MatchCase = True

        .MatchWholeWord = False

        .MatchByte = True

        .MatchWildcards = False

        .MatchSoundsLike = False

        .MatchAllWordForms = False

    End With

    Selection.Find.Execute

    If Selection.Find.Found = True Then

        Selection.Find.Execute Replace:=wdReplaceAll

        Selection.Text = Mid(str, i, 1)

         cf = cf & Mid(str, i, 1)

         End If

Next i

    MsgBox cf

使用“宏”删除WORD中的重复字

三、运行宏

使用“宏”删除WORD中的重复字

四、结果如下

使用“宏”删除WORD中的重复字

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢