Find a string in a document and delete everything after it

前端 未结 1 1178
耶瑟儿~
耶瑟儿~ 2020-12-20 03:44

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?

相关标签:
1条回答
  • 2020-12-20 04:17

    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
    
    0 讨论(0)
提交回复
热议问题