I'm trying to write a macro that will check for inconsistencies in spelling between UK and US English, using "aging"/"ageing" as an example here, which displays a messagebox if inconsistencies are found. The caveat is it needs to only search for text in the main body of work, i.e., between the words Abstract and References (both in bold, so it only catches when they've been used as headers).
The issue I'm having is with Wrap = wdFindContinue
, which seems to be extended the search outside of the range. However if I use Wrap = wdFindStop
, it doesn't work at all (and wdFindAsk
is inappropriate for the use case).
Can you see any glaring errors in the code below? Cheers
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
CodePudding user response:
Explanatory comments in code below
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
Rewritten as I would do it:
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