可用于批量替换文件夹中所有word的关键字的宏代码(可保留原格式)

使用说明

** 一、在word工具栏找到开发工具,在开发工具中点击Visual Basic**
在这里插入图片描述
如果工具栏中没有开发工具这一个功能区,需要点击工具栏中的文件→选项→自定义功能区,将开发工具勾选。

  1. 在这里插入图片描述

  2. 在这里插入图片描述

  3. 在这里插入图片描述
    二、在点击Visual Basic后,在Normal处右键→插入→模块

在这里插入图片描述
在弹出的文本框中复制宏代码并进行保存,宏代码就可以使用了
三、使用时点击工具栏中的开发工具→宏→选择哪个宏→运行,根据对话框输入相应文字
在这里插入图片描述
**当前文档的宏里应该有这两个宏,如果没有需要根据流程加载一下。**文件提取密码:fi4t

批量替换宏的演示:

宏的名称为AdvancedReplaceIncludingHeaders
预处理的文件夹为桌面的“批量替换查找演示”文件夹,文件夹内部有5个word文档,包括了页眉、页脚、表格格式的ACT内容,演示批量替换宏使QAQ文字改为NBA

1.点击word工具栏中的开发工具→宏→选中AdvancedReplaceIncludingHeaders→运行
在这里插入图片描述

2.弹出第一个对话框,这里输入需要替换的文字“QAQ”,点击确定
在这里插入图片描述

3.弹出第二个对话框,这里输入替换后的问题“NBA”,点击确定
在这里插入图片描述

4.弹出第三个对话框,选择是否区分大小写

在这里插入图片描述

5.弹出第四个对话框,选择是否全字匹配
(一般选择否,如果选择是,则会出现文本为apple,查找ap会显示查找不到的情况)
在这里插入图片描述

6.弹出第五个对话框,选择文件夹
随后宏开始运行,等待替换
在这里插入图片描述

批量查找宏的演示:

宏的名称为SearchTextInAllWordDocuments

1.点击word工具栏中的开发工具→宏→选中SearchTextInAllWordDocuments→运行
在这里插入图片描述

2.弹出对话框,是否需要区分大小写
在这里插入图片描述

3.弹出对话框,选择文件夹。
随后宏开始运行,等待查找
在这里插入图片描述

批量替换宏代码

Sub AdvancedReplaceIncludingHeaders()
    Dim folderPath As String, filePath As String
    Dim doc As Document, rng As Range
    Dim keyword1 As String, keyword2 As String
    Dim matchCase As Boolean, matchWholeWord As Boolean
    Dim originalFont As Font
    Dim replaceCount As Long, fileCount As Long
    Dim startTime As Double, resultMsg As String
    
    ' 用户输入
    keyword1 = InputBox("请输入要替换的文本:", "旧文本", "")
    If keyword1 = "" Then Exit Sub
    
    keyword2 = InputBox("请输入替换后的文本:", "新文本", "")
    If keyword2 = "" Then Exit Sub
    
    ' 搜索选项
    matchCase = (MsgBox("是否区分大小写?", vbQuestion + vbYesNo, "搜索选项") = vbYes)
    matchWholeWord = (MsgBox("是否全字匹配?", vbQuestion + vbYesNo, "搜索选项") = vbYes)
    
    ' 选择文件夹
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "选择包含Word文档的文件夹"
        If .Show <> -1 Then Exit Sub
        folderPath = .SelectedItems(1) & "\"
    End With
    
    ' 初始化
    startTime = Timer
    filePath = Dir(folderPath & "*.doc*")
    Application.ScreenUpdating = False
    
    ' 主处理循环
    Do While filePath <> ""
        fileCount = fileCount + 1
        On Error Resume Next
        Set doc = Documents.Open(folderPath & filePath, ReadOnly:=False, Visible:=False)
        
        If Err.Number = 0 Then
            replaceCount = 0
            
            ' 1. 替换主文档内容
            replaceCount = replaceCount + ReplaceInRange(doc.Content, keyword1, keyword2, matchCase, matchWholeWord)
            
            ' 2. 替换所有页眉
            Dim sec As section, hdr As HeaderFooter
            For Each sec In doc.Sections
                For Each hdr In sec.Headers
                    If hdr.Exists Then
                        replaceCount = replaceCount + ReplaceInRange(hdr.Range, keyword1, keyword2, matchCase, matchWholeWord)
                    End If
                Next hdr
                
                ' 3. 替换所有页脚
                For Each hdr In sec.Footers
                    If hdr.Exists Then
                        replaceCount = replaceCount + ReplaceInRange(hdr.Range, keyword1, keyword2, matchCase, matchWholeWord)
                    End If
                Next hdr
            Next sec
            
            ' 保存并记录结果
            doc.Close SaveChanges:=wdSaveChanges
            resultMsg = resultMsg & filePath & " - 替换 " & replaceCount & " 处" & vbCrLf
        Else
            resultMsg = resultMsg & filePath & " - [打开失败]" & vbCrLf
            Err.Clear
        End If
        On Error GoTo 0
        filePath = Dir()
    Loop
    
    ' 显示结果
    Application.ScreenUpdating = True
    timeUsed = Format((Timer - startTime) / 86400, "hh:mm:ss")
    
    MsgBox "替换完成!" & vbCrLf & _
           "处理文件: " & fileCount & " 个" & vbCrLf & _
           "总耗时: " & timeUsed & vbCrLf & vbCrLf & _
           "===== 详细结果 =====" & vbCrLf & resultMsg, _
           vbInformation, "批量替换报告"
End Sub

' 辅助函数:在指定范围内替换文本并保留格式
Function ReplaceInRange(rng As Range, findText As String, replaceText As String, _
                       matchCase As Boolean, matchWholeWord As Boolean) As Long
    Dim count As Long
    Dim originalFont As Font
    
    With rng.Find
        .ClearFormatting
        .Text = findText
        .Replacement.Text = replaceText
        .Forward = True
        .Wrap = wdFindStop
        .matchCase = matchCase
        .matchWholeWord = matchWholeWord
        .MatchWildcards = False
        
        Do While .Execute
            ' 保存原格式
            Set originalFont = rng.Font.Duplicate
            ' 执行替换
            rng.Text = replaceText
            ' 恢复原格式
            rng.Font = originalFont
            count = count + 1
            ' 移动范围继续查找
            rng.Collapse wdCollapseEnd
        Loop
    End With
    
    ReplaceInRange = count
End Function

批量查找宏代码

Sub SearchTextInAllWordDocuments()
    Dim folderPath As String
    Dim searchText As String
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim wordDoc As Word.Document
    Dim fileCount As Integer
    Dim foundCount As Integer
    Dim resultMsg As String
    Dim totalFilesWithText As Integer
    Dim totalOccurrences As Integer
    Dim matchCase As Boolean
    Dim response As Integer
    
    ' 询问是否区分大小写
    response = MsgBox("搜索时是否区分大小写?", vbQuestion + vbYesNo, "区分大小写")
    matchCase = (response = vbYes)
    
    ' 让用户选择文件夹
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "请选择要搜索的文件夹"
        If .Show = -1 Then
            folderPath = .SelectedItems(1)
        Else
            MsgBox "未选择文件夹,操作已取消。", vbInformation
            Exit Sub
        End If
    End With
    
    ' 让用户输入要搜索的文字
    searchText = InputBox("请输入要搜索的文字:", "搜索内容")
    If searchText = "" Then
        MsgBox "未输入搜索文字,操作已取消。", vbInformation
        Exit Sub
    End If
    
    ' 初始化文件系统对象
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderPath)
    
    ' 初始化结果变量
    fileCount = 0
    totalFilesWithText = 0
    totalOccurrences = 0
    resultMsg = "搜索结果:" & vbCrLf & vbCrLf
    
    ' 显示搜索选项
    resultMsg = resultMsg & "搜索选项:" & vbCrLf & _
                "区分大小写: " & IIf(matchCase, "是", "否") & vbCrLf & vbCrLf
    
    ' 遍历文件夹中的所有文件
    For Each file In folder.Files
        ' 只处理Word文档(.doc, .docx)
        If LCase(fso.GetExtensionName(file.Name)) = "doc" Or _
           LCase(fso.GetExtensionName(file.Name)) = "docx" Then
           
            fileCount = fileCount + 1
            foundCount = 0
            
            ' 打开文档
            Set wordDoc = Documents.Open(file.Path, ReadOnly:=True, Visible:=False)
            
            ' 搜索文字
            With wordDoc.Content.Find
                .Text = searchText
                .matchCase = matchCase ' 使用用户选择的区分大小写选项
                .matchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Forward = True
                .Wrap = wdFindStop
                
                Do While .Execute
                    foundCount = foundCount + 1
                Loop
            End With
            
            ' 记录结果
            If foundCount > 0 Then
                resultMsg = resultMsg & file.Name & ": " & foundCount & " 处" & vbCrLf
                totalFilesWithText = totalFilesWithText + 1
                totalOccurrences = totalOccurrences + foundCount
            End If
            
            ' 关闭文档
            wordDoc.Close SaveChanges:=False
        End If
    Next file
    
    ' 显示结果
    resultMsg = resultMsg & vbCrLf & "统计结果:" & vbCrLf & _
                "搜索文件夹: " & folderPath & vbCrLf & _
                "搜索内容: " & searchText & vbCrLf & _
                "区分大小写: " & IIf(matchCase, "是", "否") & vbCrLf & _
                "总文件数: " & fileCount & vbCrLf & _
                "包含搜索内容的文件数: " & totalFilesWithText & vbCrLf & _
                "总出现次数: " & totalOccurrences
    
    MsgBox resultMsg, vbInformation, "搜索完成"
    
    ' 清理对象
    Set wordDoc = Nothing
    Set folder = Nothing
    Set fso = Nothing
End Sub

' 辅助函数:IIf函数模拟
Function IIf(expr As Boolean, trueVal As Variant, falseVal As Variant) As Variant
    If expr Then
        IIf = trueVal
    Else
        IIf = falseVal
    End If
End Function


### 如何在 Windows 上将 Node.js 升级到指定版本 对于希望在 Windows 平台上升级 Node.js 到特定版本的用户来说,使用 NVM (Node Version Manager) 是一种推荐的方法[^1]。下面提供具体的操作指南。 #### 安装适用于 Windows 的 NVM 首先需要安装 nvm-windows 工具来管理不同版本的 Node.js: 1. 访问 [nvm-windows 发布页面](https://github.com/coreybutler/nvm-windows/releases),下载最新版的 `nvm-setup.zip` 文件并解压。 2. 运行解压缩后的 `.exe` 文件完成安装过程。 3. 打开命令提示符窗口验证安装是否成功,输入 `nvm version` 应显示已安装的 nvm 版本号。 #### 使用 NVM 安装或切换 Node.js 版本 一旦有了 nvm-windows 后,就可以方便地安装所需的 Node.js 版本以及设置默认使用的版本: - 查询可用的远程版本列表:`nvm list available` - 安装某个特定版本(例如 v14.17.0):`nvm install 14.17.0` - 设置当前会话中的活动版本为刚安装的那个版本:`nvm use 14.17.0` - 将该版本设为全局默认版本:`nvm alias default 14.17.0` 以上操作完成后,可以通过运行 `node -v` 来确认现在正在使用的 Node.js 是否已经是所期望的目标版本。 ```bash # 验证安装情况 nvm --version # 显示所有本地已安装版本 nvm list # 更新至目标版本 nvm install <target_version> nvm use <target_version> ``` 为了确保环境配置正确无误,在每次更改 Node.js 版本之后建议重启任何打开的应用程序和服务端口以便使新的版本生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值