vba - 在文档中查找字符串并删除其后的所有内容

标签 vba ms-word ms-office range document

我想在word文档中找到一个字符串并删除它之后的所有内容。

在不使用 Selection 的情况下执行此操作的最佳方法是什么?目的?

最佳答案

使用 Range对象。直接脱离 Word 2003 帮助:

If you've gotten to the Find object from the Range object, the selection isn't changed when text matching the find criteria is found, but the Range object is redefined. The following example locates the first occurrence of the word "blue" in the active document. If "blue" is found in the document, myRange is redefined


Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="blue", _
    Forward:=True
If myRange.Find.Found = True Then 

现在使用 SetRange方法Range对象使范围的开头成为您搜索的字符串结尾之后的下一个字符,并使范围的结尾成为文档的结尾:
myRange.SetRange (myRange.End + 1), ActiveDocument.Content.End

(待办事项:当您的字符串是文档中的最后一个内容时,您需要处理这种情况)

要删除内容:
myRange.Delete

关于vba - 在文档中查找字符串并删除其后的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/497624/

相关文章:

VBA - 在串联单元格之间插入空格

vba - 给定范围内哪些单元格为空

vba - 电子邮件宏每 40 - 50 封电子邮件暂停一次

vba - 通过 GetTickCount 获取代码运行时并以特定方式格式化它

vb.net - 打开文件夹中的每个 docx 文件并另存为其他文件类型

VBA:Word 2010 - 使用 VBA 关闭页眉和页脚工具

excel - 如何在 Excel 中用特定值替换单元格?

c# - 使用 Microsoft.Office.Interop 向 MS Word 表格添加一行

pdf - 使用PowerShell脚本访问Word 'Save As'对话框

ios - 在 iOS 上的 Excel 应用程序中打开链接的 XLSM 文件