宏在 vbNo 之后停止搜索同一个词
Macro stopped searching for the same word after vbNo
美好的一天VBA大师们
我可以就我的代码寻求您的帮助吗?
正如您在下面看到的,我的代码旨在仅搜索特定 header 样式的单词,然后将其转换为小写。
我的问题是,一旦我在消息框中选择 "No",我的宏就会停止在下一个 header 中搜索相同的词。
我的文档确实有多个 header,我需要搜索同一个词直到文档的最后一个 header。
我遵循 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
美好的一天,欢迎加入。
以下是一些应该进行的编辑:
- 在您的代码找到某个内容后,它会选择它。所以,当你问它
要再次搜索,它会在上次搜索结果的选择范围内搜索。您已经有了在
vbYes
情况下执行此操作的 Selection.Collapse
语句。你也只需要在 vbNo
的情况下应用它。
- 为确保代码将搜索一个词直到文档末尾并且不会重新开始,您应该将
.Wrap = wdFindContinue
更改为 .Wrap = wdFindStop
。
- 为了确保代码从文档的开头到结尾搜索每个单词,您应该将
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
美好的一天VBA大师们
我可以就我的代码寻求您的帮助吗?
正如您在下面看到的,我的代码旨在仅搜索特定 header 样式的单词,然后将其转换为小写。
我的问题是,一旦我在消息框中选择 "No",我的宏就会停止在下一个 header 中搜索相同的词。
我的文档确实有多个 header,我需要搜索同一个词直到文档的最后一个 header。
我遵循 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
美好的一天,欢迎加入。
以下是一些应该进行的编辑:
- 在您的代码找到某个内容后,它会选择它。所以,当你问它
要再次搜索,它会在上次搜索结果的选择范围内搜索。您已经有了在
vbYes
情况下执行此操作的Selection.Collapse
语句。你也只需要在vbNo
的情况下应用它。 - 为确保代码将搜索一个词直到文档末尾并且不会重新开始,您应该将
.Wrap = wdFindContinue
更改为.Wrap = wdFindStop
。 - 为了确保代码从文档的开头到结尾搜索每个单词,您应该将
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