在指定范围内应用 Find.Wrap

发布于 2025-01-11 06:06:51 字数 1465 浏览 0 评论 0原文

我正在尝试检查英国英语和美国英语之间的拼写不一致,例如“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

I'm trying to check for inconsistencies in spelling between UK and US English, "aging"/"ageing" as an example, to display a message box if inconsistencies are found.

I need 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 used as headers).

Wrap = wdFindContinue seems to be extending the search outside of the range.
Wrap = wdFindStop doesn't work.
wdFindAsk is inappropriate for the use case.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-01-18 06:06:51

下面代码中的解释性注释

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文