我想在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/