在文档中查找字符串并删除其后的所有内容 [英] Find a string in a document and delete everything after it
问题描述
我想在Word文档中找到一个字符串并删除其后的所有内容.
I want to find a string in a word document and delete everything after it.
在不使用Selection
对象的情况下执行此操作的最佳方法是什么?
What is the best way to do this without using the Selection
object?
推荐答案
请改用Range
对象.直接失去Word 2003的帮助:
Use a Range
object instead. Straight outta the Word 2003 help:
如果您已找到查找"对象 从范围对象中,选择 当文字与 找到条件,但范围 对象被重新定义.以下 该示例查找第一个出现的位置 活动中的蓝色"一词 文档.如果在 文档中,myRange已重新定义
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
现在使用该Range
对象的SetRange
方法,使范围的开头成为您搜索的字符串结尾之后的下一个字符,并使范围的结尾成为文档的结尾:
Now use the SetRange
method of that Range
object to make the start of the range be the next character after the end of the string you searched for and make the end of the range be the end of the document:
myRange.SetRange (myRange.End + 1), ActiveDocument.Content.End
(待办事项:当字符串是文档中的最后一件事时,您需要处理这种情况)
(TODO: You'll need to deal with the case when your string is the last thing in the document)
要删除内容:
myRange.Delete
这篇关于在文档中查找字符串并删除其后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!