宏在vbNo之后停止搜索同一个单词



祝VBA大师好运

我可以就我的代码向您寻求帮助吗?

正如您在下面看到的,我的代码只搜索特定标题样式的单词,然后将其转换为小写。

我的问题是,一旦我在消息框中选择"否",我的宏就停止在下一个标题中搜索同一个单词。

我的文档确实有多个页眉,我需要搜索同一个单词,直到文档的最后一个页眉。

我遵循SOP,所以我无法将所有单词转换为小写。

希望你能帮我解决我的问题。提前谢谢。

Sub ChangeCase1()
Dim StrFind As String, StrRepl As String
Dim i As Long
StrFind = "And,Aboard,About,Above,Across,After,Against,Along,Alongside,Amid,Amidst,Among,Around,As,Aside,At,Athwart,Atop,Barring,Before,BehindBelow,Off,On,Onto,Opposite,Out,Outside,Beneath,Beside,Besides,Between,Beyond,But,By,Circa,Concerning,Despite,Down,During,Except,Following,For,From,In,Inside,Into,Like,Mid,Minus,Near,Next,Notwithstanding,Of,Worth,Over,Pace,Past,Per,Plus,Regarding,Round,Since,Than,Through,Throughout,Till,Times,To,Toward,Towards,Under,Underneath,Unlike,Until,Up,Upon,Versus,Via,With,Within,Without"
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrFind, ","))
With Selection.Find
.ClearFormatting
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchCase = True
.Text = Split(StrFind, ",")(i)
.Style = ActiveDocument.Styles("K-Heading Level 1")
.Execute
While .Found
If MsgBox("Replace " & Split(StrFind, ",")(i), vbYesNo) = vbYes Then
Selection.Range.Case = wdLowerCase
Selection.Collapse Direction:=wdCollapseEnd
.Execute
Else
GoTo Continue
End If
Wend
End With
Continue:
Next i
Call ChangeCase2
End Sub

祝大家愉快,欢迎登机。

以下是一些应该进行的编辑:

  1. 当你的代码找到某个东西后,它会选择它。所以,当你问它时若要再次搜索,它会从上次搜索结果中搜索所选内容。您已经有了在vbYes情况下执行此操作的Selection.Collapse语句。您只需要将其应用于vbNo的情况
  2. 为了确保代码将搜索一个单词,直到文档结束,并且不会重新开始,您应该将.Wrap = wdFindContinue更改为.Wrap = wdFindStop
  3. 为了确保代码能够搜索从文档开头到结尾的每个单词,您应该将With Selection.Find更改为With ActiveDocument.Content.Find

所以,你的代码应该是:

Sub ChangeCase1()
Dim StrFind As String, StrRepl As String
Dim i As Long
StrFind = "And,Aboard,About,Above,Across,After,Against,Along,Alongside,Amid,Amidst,Among,Around,As,Aside,At,Athwart,Atop,Barring,Before,BehindBelow,Off,On,Onto,Opposite,Out,Outside,Beneath,Beside,Besides,Between,Beyond,But,By,Circa,Concerning,Despite,Down,During,Except,Following,For,From,In,Inside,Into,Like,Mid,Minus,Near,Next,Notwithstanding,Of,Worth,Over,Pace,Past,Per,Plus,Regarding,Round,Since,Than,Through,Throughout,Till,Times,To,Toward,Towards,Under,Underneath,Unlike,Until,Up,Upon,Versus,Via,With,Within,Without"    
Set RngTxt = Selection.Range
For i = 0 To UBound(Split(StrFind, ","))
With ActiveDocument.Content.Find
.ClearFormatting
.Wrap = wdFindStop
.Forward = True
.Format = True
.MatchCase = True
.Text = Split(StrFind, ",")(i)
.Style = ActiveDocument.Styles("K-Heading Level 1")
.Execute
While .Found
If MsgBox("Replace " & Split(StrFind, ",")(i), vbYesNo) = vbYes Then
Selection.Range.Case = wdLowerCase
End If
Selection.Collapse Direction:=wdCollapseEnd
.Execute
Wend
End With
Next i
'Call ChangeCase2
End Sub

最新更新