如何在Word文档中的两个书签之间选择或删除文本?
您知道如何在Word文档中的两个书签之间选择或删除内容吗? 本文将向您展示处理它的方法。
使用VBA选择/删除两个书签之间的文本
假设文档中有两个书签,如下面的屏幕快照所示,您可以使用以下VBA代码选择或删除这两个书签之间的所有内容。
1.在文档中,按 其他 + F11 键打开 Microsoft Visual Basic应用程序 窗口。
2.在 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 模块,然后将以下代码复制到“模块”窗口中。
要选择或删除两个书签之间的文本,请使用以下代码:
VBA代码:在Word中的两个指定的书签之间选择文本:
Sub SelectBetweenBookmarks()
Dim xRange As Range
Dim xBMone, xBMtwo As Bookmark
Dim xBookMarkOne, xBookMarkTwo As String
On Error Resume Next
Set xRange = ActiveDocument.Content
xBookMarkOne = InputBox("Please enter the start bookmark:", "Kutools for Word")
xBookMarkTwo = InputBox("Please enter the end bookmark:", "Kutools for Word")
Set xBMone = ActiveDocument.Bookmarks(xBookMarkOne)
Set xBMtwo = ActiveDocument.Bookmarks(xBookMarkTwo)
If xBMone Is Nothing Or xBMtwo Is Nothing Then
MsgBox "Please enter the correct bookmark name", vbInformation, "KuTools for Word"
Exit Sub
End If
xRange.Start = xBMone.Range.End
xRange.End = xBMtwo.Range.Start
xRange.Select
End Sub
VBA代码:删除Word中两个指定书签之间的文本:
Sub DeleteBetweenBookmarks ()
Dim xRange As Range
Dim xBMone, xBMtwo As Bookmark
Dim xBookMarkOne, xBookMarkTwo As String
On Error Resume Next
Set xRange = ActiveDocument.Content
xBookMarkOne = InputBox("Please enter the start bookmark:", "Kutools for Word")
xBookMarkTwo = InputBox("Please enter the end bookmark:", "Kutools for Word")
Set xBMone = ActiveDocument.Bookmarks(xBookMarkOne)
Set xBMtwo = ActiveDocument.Bookmarks(xBookMarkTwo)
If xBMone Is Nothing Or xBMtwo Is Nothing Then
MsgBox " Please enter the correct bookmark name ", vbInformation, "KuTools for Word"
Exit Sub
End If
xRange.Start = xBMone.Range.End
xRange.End = xBMtwo.Range.Start
xRange.Delete
End Sub
3。 按 F5 键来运行代码。
4.在第一 Kutools for Word 对话框,在文本框中输入起始书签名称,然后单击 OK.
5.在第二 Kutools for Word 对话框,输入结束书签,然后单击 OK。 看截图:
备注:如果您输入的书签名称在文档中不存在,则会弹出另一个 Kutools for Word 对话框,提醒您输入正确的书签名称。
然后,将立即选择或删除指定书签之间的所有内容。 查看屏幕截图。
在两个书签之间选择文本:
删除两个书签之间的文本。
最佳办公生产力工具
Kutools for Word - 通过 Over 提升您的文字体验 100 显着特点!
🤖 Kutools 人工智能助手:用人工智能改变你的写作 - 生成内容 / 重写文本 / 总结文件 / 查询资料 基于文档,全部在Word中
📘 文档掌握: 分页 / 合并文件 / 以各种格式导出选择(PDF/TXT/DOC/HTML...) / 批量转换为PDF / 将页面导出为图像 / 一次打印多个文件...
✏ 内容编辑: 批量查找和替换 跨多个文件 / 调整所有图片的大小 / 转置表行和列 / 将表格转换为文字...
🧹 轻松清洁: 移开 多余的空间 / 分节符 / 所有标题 / 文本框 / 超链接 / 如需更多拆卸工具,请前往我们的 删除组...
➕ 创意插入: 插 千位分隔符 / 复选框 / 单选按钮 / 扫码支付 / 条码 / 对角线表 / 公式标题 / 图片说明 / 表标题 / 多张图片 / 发现更多 插入组...
🔍 精准选择:精确定位 特定页面 / 表 / 形状 / 标题段落 / 增强导航功能 更多 选择功能...
⭐ 星级增强: 快速导航至任何位置 / 自动插入重复文本 / 在文档窗口之间无缝切换 / 11 转换工具...