【问题标题】:Applying Find.Wrap in a specified Range在指定范围内应用 Find.Wrap
【发布时间】:2022-06-30 05:42:40
【问题描述】:

我正在尝试检查英国和美国英语之间的拼写不一致,例如“aging”/“ageing”,如果发现不一致,则显示一个消息框。

我只需要在工作主体中搜索文本,即在“摘要”和“参考”这两个词之间(都是粗体,所以它只在用作标题时才被捕获)。

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    下面代码中的解释性 cmets

    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
    

    【讨论】:

    • 这是一个绝妙的答案,谢谢。您在顶部的示例非常有效。我尝试了您的第二个示例,它可以工作,但即使只找到“老化”,msgbox 仍会被触发 - 知道为什么会这样吗?
    • @timjack - 如果只有一个拼写,我不会收到任何一个版本的消息框。
    • 这很奇怪,可能是我的计算机或我的 Word 副本的错误。哦,好吧,第一个效果很好,解释都非常有用,再次欢呼:)
    猜你喜欢
    • 2014-02-04
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多