将 Find.Execute Replace:=wdReplaceOne 布尔值 return 值保存到变量
Save Find.Execute Replace:=wdReplaceOne boolean return value to variable
如何将 Selction.Find.Execute 中的 return 值保存到布尔变量中。这是我的代码片段,我收到“myBool = Selection.Find.Execute Replace:=wdReplaceOne”
的编译错误
Dim myBool As Boolean
myBool = True
Do While myBool
Selection.Find.Text = "myText"
Selection.Find.Replacement.Text = replacementText //More logic to change this variable not shown
myBool = Selection.Find.Execute Replace:=wdReplaceOne
Loop
基本上,我有一个循环来进行搜索和替换,但我试图在不再存在搜索文本时停止循环。
如果您想将其用作函数(returns 某物),则需要包含括号。
Dim myBool As Boolean
myBool = True
Do While myBool
Selection.Find.Text = "myText"
Selection.Find.Replacement.Text = replacementText //More logic to change this variable not shown
myBool = Selection.Find.Execute(Replace:=wdReplaceOne)
Loop
Timothy 也提出了关于使用 Replace:=wdReplaceAll
的有效观点。如果你能避免使用循环,它总是 better/faster,但这不是问题。
如果你设置:
.Wrap = wdFindStop
不需要测试。例如:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = InputBox("What is the Text to Find")
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
i = i + 1
'Do something
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
并且,如果您想将进程限制在选定的范围内:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Rng As Range
Set Rng = Selection.Range
With Selection.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = InputBox("What is the Text to Find")
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
i = i + 1
'Do something
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
如何将 Selction.Find.Execute 中的 return 值保存到布尔变量中。这是我的代码片段,我收到“myBool = Selection.Find.Execute Replace:=wdReplaceOne”
的编译错误Dim myBool As Boolean
myBool = True
Do While myBool
Selection.Find.Text = "myText"
Selection.Find.Replacement.Text = replacementText //More logic to change this variable not shown
myBool = Selection.Find.Execute Replace:=wdReplaceOne
Loop
基本上,我有一个循环来进行搜索和替换,但我试图在不再存在搜索文本时停止循环。
如果您想将其用作函数(returns 某物),则需要包含括号。
Dim myBool As Boolean
myBool = True
Do While myBool
Selection.Find.Text = "myText"
Selection.Find.Replacement.Text = replacementText //More logic to change this variable not shown
myBool = Selection.Find.Execute(Replace:=wdReplaceOne)
Loop
Timothy 也提出了关于使用 Replace:=wdReplaceAll
的有效观点。如果你能避免使用循环,它总是 better/faster,但这不是问题。
如果你设置:
.Wrap = wdFindStop
不需要测试。例如:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = InputBox("What is the Text to Find")
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
i = i + 1
'Do something
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
并且,如果您想将进程限制在选定的范围内:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Rng As Range
Set Rng = Selection.Range
With Selection.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = InputBox("What is the Text to Find")
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .InRange(Rng) = False Then Exit Do
i = i + 1
'Do something
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub