word跨文件批量替换

开发工具 -> vbs编辑

新建一个模块

Sub Word内容替换()

    Dim 筛选 As FileDialog
    
    Dim 文件名, 文件 As Document
    
    Dim pd, 计数 As Integer
    
    Set 筛选器 = Application.FileDialog(msoFileDialogFilePicker)
    
    With 筛选器
    
        .AllowMultiSelect = True
        
        pd = MsgBox("请选择需要处理的文件:(可多选)", vbOKCancel + vbInformation, "选择文件")
        
        If pd = vbCancel Then
            MsgBox "选择已取消,将退出程序!"
            Exit Sub
        End If
        
        .Show
    
    End With

For Each 文件名 In 筛选器.SelectedItems
        If Not Right(文件名, Len(文件名) - InStrRev(文件名, ".")) Like "doc*" Then GoTo 1
        Set 文件 = Documents.Open(文件名)
        With 文件.Content.Find
            .ClearFormatting    ' 清除文本和段落格式
            .MatchWildcards = True  ' 查找内容可以包括通配符
            .Wrap = wdFindStop      '
            .Text = "要查找的内容" '想被替换的文字
            With .Replacement
                .ClearFormatting
                .Text = "替换的内容"  '替换的内容
            End With
            .Execute Replace:=wdReplaceAll
        End With
        文件.Close wdSaveChanges
        计数 = 计数 + 1
        Set 文件 = Nothing
1:  Next
    Set 筛选器 = Nothing
    MsgBox "已完成!共处理了" & 计数 & "个文件。"
End Sub

 

来源:VBA一键批量替换word中的内容_哔哩哔哩_bilibili

posted @ 2023-06-13 11:13  紫英626  阅读(35)  评论(0编辑  收藏  举报

紫英