我想在word文档中找到一个字符串并删除它后面的所有内容。
在不使用
Selection
对象的情况下执行此操作的最佳方法是什么?
请使用
Range
对象代替。直接脱离 Word 2003 帮助:
如果您已找到“查找”对象 从 Range 对象中,选择 当文本匹配时不会改变 查找条件已找到,但范围 对象被重新定义。下列 示例定位第一次出现的位置 活动中“蓝色”一词的 文档。如果在 文档,myRange 被重新定义
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
(TODO:当您的字符串是文档中的最后一个内容时,您需要处理这种情况)
删除内容:
myRange.Delete
子删除()
Dim findtext As String
findtext = "$_delete" 'Your word
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute
If (Selection.Find.Execute(findtext) = True) Then
With ActiveDocument.Content.Find
With .Replacement
.ClearFormatting
.text = ""
End With
.Execute Replace:=wdReplaceAll
End With
Selection.delete Count:='here the number of contents you want to delete
End If
结束子