在指定范围内使用宏时遇到正确的 Find.Wrap 属性
Struggling with the correct Find.Wrap property when using a macro in a specified Range
我正在尝试编写一个宏来检查英国英语和美国英语之间的拼写不一致,这里以“aging”/“ageing”为例,如果发现不一致,它会显示一个消息框。需要注意的是,它只需要在主要 body 工作中搜索文本,即在 Abstract 和 References 之间(都是粗体,所以它只在它们被用作 headers 时捕获).
我遇到的问题是 Wrap = wdFindContinue
,它似乎将搜索范围扩展到了范围之外。但是,如果我使用 Wrap = wdFindStop
,它根本不起作用(并且 wdFindAsk
不适合用例)。
您能在下面的代码中看到任何明显的错误吗?干杯
Sub inconsistencyCheck()
Dim myrange As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
a = 0
b = 0
'search for abstract
With Selection.Find
.Font.Bold = True
.Text = "Abstract"
.Wrap = wdFindContinue
.Execute
End With
myrange.Start = Selection.Start
'search for references
With Selection.Find
.Font.Bold = True
.Text = "References"
.Wrap = wdFindContinue
.Execute
End With
myrange.End = Selection.Start
myrange.Select
'search for inconsistencies
With myrange.Find
.MatchWholeWord = False
.Wrap = wdFindContinue
.Execute findtext:="aging"
.Format = True
.Forward = True
If .Found = True Then
a = 1
End If
.MatchWholeWord = False
.Wrap = wdFindContinue
.Execute findtext:="ageing"
.Format = True
.Forward = True
If .Found = True Then
b = 1
End If
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub
下面代码中的解释性注释
Sub inconsistencyCheck()
Dim myrange As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
a = 0
b = 0
'search for abstract
With Selection.Find
.Font.Bold = True
.Text = "Abstract"
.Wrap = wdFindContinue
.Execute
End With
myrange.Start = Selection.Start
'search for references
With Selection.Find
.Font.Bold = True
.Text = "References"
.Wrap = wdFindContinue
.Execute
End With
myrange.End = Selection.Start
'myrange.Select
'search for inconsistencies
With myrange.Find
.MatchWholeWord = False
.Wrap = wdFindStop
.Forward = True 'needs to be set before execution
'myrange will be redefined to the found match if successful so subsequent find won't succeed
'use a duplicate of myrange for the first execution.
'Duplicate needs to be used first or you'll simply duplicate the found range
If myrange.Duplicate.Find.Execute(findtext:="aging") Then a = 1
If .Execute(findtext:="ageing") Then b = 1
'.Format = True - not required you're trying to find text not formatting
'not required as .Execute returns a boolean
'If .Found = True Then
' a = 1
'End If
'find parameters are already set so don't need to set them again
' .MatchWholeWord = False
' .Wrap = wdFindContinue
' .Execute findtext:="ageing"
' .Format = True
' .Forward = True
' If .Found = True Then
' b = 1
' End If
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub
按照我的意愿重写:
Sub inconsistencyCheck()
Dim myrange As Range, findIn As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
'establish range to search
With myrange.Find
.Font.Bold = True
.Wrap = wdFindStop
If .Execute(findtext:="Abstract") Then Set findIn = myrange.Duplicate
myrange.Collapse wdCollapseEnd
myrange.End = ActiveDocument.Content.End
If .Execute(findtext:="References") Then findIn.End = myrange.Start
End With
findIn.Select
'search for inconsistencies
With findIn.Find
.MatchWholeWord = False
.Wrap = wdFindContinue
.Forward = True
If findIn.Duplicate.Find.Execute(findtext:="ageing") Then b = 1
If .Execute(findtext:="aging") Then a = 1
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub
我正在尝试编写一个宏来检查英国英语和美国英语之间的拼写不一致,这里以“aging”/“ageing”为例,如果发现不一致,它会显示一个消息框。需要注意的是,它只需要在主要 body 工作中搜索文本,即在 Abstract 和 References 之间(都是粗体,所以它只在它们被用作 headers 时捕获).
我遇到的问题是 Wrap = wdFindContinue
,它似乎将搜索范围扩展到了范围之外。但是,如果我使用 Wrap = wdFindStop
,它根本不起作用(并且 wdFindAsk
不适合用例)。
您能在下面的代码中看到任何明显的错误吗?干杯
Sub inconsistencyCheck()
Dim myrange As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
a = 0
b = 0
'search for abstract
With Selection.Find
.Font.Bold = True
.Text = "Abstract"
.Wrap = wdFindContinue
.Execute
End With
myrange.Start = Selection.Start
'search for references
With Selection.Find
.Font.Bold = True
.Text = "References"
.Wrap = wdFindContinue
.Execute
End With
myrange.End = Selection.Start
myrange.Select
'search for inconsistencies
With myrange.Find
.MatchWholeWord = False
.Wrap = wdFindContinue
.Execute findtext:="aging"
.Format = True
.Forward = True
If .Found = True Then
a = 1
End If
.MatchWholeWord = False
.Wrap = wdFindContinue
.Execute findtext:="ageing"
.Format = True
.Forward = True
If .Found = True Then
b = 1
End If
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub
下面代码中的解释性注释
Sub inconsistencyCheck()
Dim myrange As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
a = 0
b = 0
'search for abstract
With Selection.Find
.Font.Bold = True
.Text = "Abstract"
.Wrap = wdFindContinue
.Execute
End With
myrange.Start = Selection.Start
'search for references
With Selection.Find
.Font.Bold = True
.Text = "References"
.Wrap = wdFindContinue
.Execute
End With
myrange.End = Selection.Start
'myrange.Select
'search for inconsistencies
With myrange.Find
.MatchWholeWord = False
.Wrap = wdFindStop
.Forward = True 'needs to be set before execution
'myrange will be redefined to the found match if successful so subsequent find won't succeed
'use a duplicate of myrange for the first execution.
'Duplicate needs to be used first or you'll simply duplicate the found range
If myrange.Duplicate.Find.Execute(findtext:="aging") Then a = 1
If .Execute(findtext:="ageing") Then b = 1
'.Format = True - not required you're trying to find text not formatting
'not required as .Execute returns a boolean
'If .Found = True Then
' a = 1
'End If
'find parameters are already set so don't need to set them again
' .MatchWholeWord = False
' .Wrap = wdFindContinue
' .Execute findtext:="ageing"
' .Format = True
' .Forward = True
' If .Found = True Then
' b = 1
' End If
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub
按照我的意愿重写:
Sub inconsistencyCheck()
Dim myrange As Range, findIn As Range
Dim a As Integer
Dim b As Integer
Set myrange = ActiveDocument.Range
'establish range to search
With myrange.Find
.Font.Bold = True
.Wrap = wdFindStop
If .Execute(findtext:="Abstract") Then Set findIn = myrange.Duplicate
myrange.Collapse wdCollapseEnd
myrange.End = ActiveDocument.Content.End
If .Execute(findtext:="References") Then findIn.End = myrange.Start
End With
findIn.Select
'search for inconsistencies
With findIn.Find
.MatchWholeWord = False
.Wrap = wdFindContinue
.Forward = True
If findIn.Duplicate.Find.Execute(findtext:="ageing") Then b = 1
If .Execute(findtext:="aging") Then a = 1
End With
If a = 1 And b = 1 Then
MsgBox "Both spellings of ageing found, please revise"
End If
End Sub