如何删除开始和结束vba字之间的文本

时间:2015-12-01 12:32:39

标签: vba ms-word word-vba

如何在起始单词和结束单词之间删除文本。

我有大约100万个单词的大量文本摘要,我想创建一个VBA脚本,它将删除所有不需要的文本。

幸运的是,我有关键词来查找并删除那些关键词之后的所有文本,直到我想要输入的特定终点。

我需要一个可以找到这些关键词的程序,并将它们作为起始单词,然后将结束单词作为结束位置,并删除它们之间的所有文本。如果单词位于段落中,我想删除该段落。

下面的程序可以完成我正在寻找的所有内容,除了它无法遍历文档并将其用于具有相同开始和结束位置的其他消息。

Sub SelectRangeBetween()


Selection.HomeKey Unit:=wdStory
'Selection.TypeText Text:="hello"

 ' The Real script
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
    .Execute findtext:="From: Research.TA@traditionanalytics.com", Forward:=True, Wrap:=wdFindStop 'this will initiate the start word
    Set myrange = Selection.Range
    myrange.End = ActiveDocument.Range.End
    myrange.Start = myrange.Start
    myrange.End = myrange.End + InStr(myrange, "This message has been scanned ") ' this will initiate the end word
    myrange.Select

    'Selection.Delete
End With
End Sub

1 个答案:

答案 0 :(得分:0)

下面的脚本将搜索您的两个关键词,并选择从第一个关键词的开头到第二个关键词的结尾的范围。只需删除'即可删除范围。

Paragraph

要选择关键字所在的Sub SomeOtherSub() Dim StartWord As String, EndWord As String Dim Find1stRange As Range, ParagraphRange As Range 'Setting up the Ranges Set Find1stRange = ActiveDocument.Range Set ParagraphRange = ActiveDocument.Range 'Set your Start and End Find words here to cleanup the script StartWord = "From: Research.TA@traditionanalytics.com" EndWord = "This message has been scanned " 'Starting the Find First Word With Find1stRange.Find .Text = StartWord .Replacement.Text = "" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False 'Execute the Find Do While .Execute 'If Found then do extra script If .Found = True Then 'Having these Selections during testing is benificial to test your script 'Find1stRange.Select 'Setting the Paragraph range Set ParagraphRange = Find1stRange.Paragraphs(1).Range 'Having these Selections during testing is benificial to test your script ParagraphRange.Select 'Deleting the paragraph 'FoundParagraph.Delete End If 'Ending the If Find1stRange .Found = True Loop 'Ending the Do While .Execute Loop End With 'Ending the Find1stRange.Find With Statement End Sub ,请参阅以下内容:

rle