在文档中查找一个字符串并删除它之后的所有内容 [英] Find a string in a document and delete everything after it

查看:38
本文介绍了在文档中查找一个字符串并删除它之后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to find a string in a word document and delete everything after it.

What is the best way to do this without using the Selection object?

解决方案

Use a Range object instead. Straight outta the Word 2003 help:

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 

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)

To delete the contents:

myRange.Delete

这篇关于在文档中查找一个字符串并删除它之后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆