0
我有一個程序,它突出顯示開始點和結束點之間的所有單詞,並循環遍歷解壓縮以找到相同的條件。程序運行良好,但不停止循環。我不得不中斷正在運行的程序來停止它。有人可以幫我寫一個條件,說明是否達到了結束條件,並且開始和結束條件不存在,停止程序。我需要爲我的程序創建一個停止條件
Sub SomeSub1()
Dim StartWord As String, EndWord As String
Dim Find1stRange As range, FindEndRange As range
Dim DelRange As range, DelStartRange As range, DelEndRange As range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'Setting up the Ranges
Set Find1stRange = ActiveDocument.range
Set FindEndRange = ActiveDocument.range
Set DelRange = ActiveDocument.range
'Set your Start and End Find words here to cleanup the script
StartWord = "From: Yussuf Ismail"
EndWord = "Kind regards"
'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
'Setting the Found range to the DelStartRange
Set DelStartRange = Find1stRange
'Having these Selections during testing is benificial to test your script
DelStartRange.Select
'Setting the FindEndRange up for the remainder of the document form the end of the StartWord
FindEndRange.Start = DelStartRange.End
FindEndRange.End = ActiveDocument.Content.End
'Having these Selections during testing is benificial to test your script
FindEndRange.Select
'Setting the Find to look for the End Word
With FindEndRange.Find
.Text = EndWord
.Execute
'If Found then do extra script
If .Found = True Then
'Setting the Found range to the DelEndRange
Set DelEndRange = FindEndRange
'Having these Selections during testing is benificial to test your script
DelEndRange.Select
End If
End With
'Selecting the delete range
DelRange.Start = DelStartRange.Start
DelRange.End = DelEndRange.End
'Having these Selections during testing is benificial to test your script
DelRange.Select
DelRange.HighlightColorIndex = wdPink
'Remove comment to actually 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
我建議您使用布爾變量來追蹤.Execute是否成功。實際上,「Do」正在測試原始的.Execute,而不是在循環中執行的那個。如果您測試每次應該幫助時設置爲.Execute的布爾值。如果.Found - 使用相同的布爾變量,那更可靠。 –
好的一段代碼@jaydub(哈哈以爲它看起來很熟悉)。 –