【问题标题】:How to find a word in a document from the current selection onwards如何从当前选择开始在文档中查找单词
【发布时间】:2018-12-21 20:13:47
【问题描述】:

我有一段代码在文档中从当前选择到结尾搜索单词。这样做的目的是在下次运行时找到下一个实例,依此类推。

它可以正常工作,直到它在表格中找到一个单词,此时它不会在该条目之后找到任何内容。我需要能够在表格和文本中找到单词。它还作为用户表单中的函数运行(无模式运行),等待用户输入然后提供不同的单词,循环并根据用户输入执行操作。所以我不相信我可以在查找部分运行我的其他代码(尽管我很高兴得到纠正)。

Sub test1()

Dim list() As String
Dim wrd As String
Dim mrk As Integer

wrd = "ABC" 'Get next word from list

'set range to search as from current selection (previously found) to end of document
Dim DocRng
Set DocRng = ActiveDocument.Range(Start:=Selection.End, End:=ActiveDocument.Content.End)

mrk = Selection.End 'Mark end of previously found instance (current selection)

With DocRng.Find 'Find next instance of word and select it
     .Text = wrd
     .MatchCase = True
     .Forward = True
     .Execute
     DocRng.Select
End With

If Selection.End = mrk Then 'If selection hasn't changed inform user and go to start of document
    MsgBox ("Reached end of document.")
    Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=0
End If

tmp = Selection.Text 'Save currently selected text

End Sub

如何让它找到表格后面的条目?

【问题讨论】:

标签: vba ms-word


【解决方案1】:

您可以在查找/替换循环中运行其他代码,使用如下代码:

Sub Demo()
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = InputBox("What is the Text to Find")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    .Select
    Select Case MsgBox("Replace this one?", vbYesNoCancel)
      Case vbCancel: Exit Sub
      Case vbYes: .Text = InputBox("Replacement text")
      Case Else
    End Select
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
End Sub

此类代码不受表格影响。

【讨论】:

  • 谢谢,但我目前使用的用户表单有 5 个按钮(可能会更多),而且我还在无模式运行它,让用户手动编辑它。据我所知,消息框无法做到这一点。
  • @Kagekiba 你试过了吗?无论如何,您都可以修改代码,以便所有输入和响应都从用户表单完成。我发布的是“概念证明”,而不是完整的解决方案 - 特别是因为您没有为此提供足够的细节。
  • 我确实尝试过,包括使用用户表单而不是消息框,但它不会让我在不运行的情况下无模式运行它(我之前在问题中忘记提到了)到最后一个实例。该代码非常适合它的功能,但我正在寻找一种在当前选择之后找到下一个实例的方法,并且不需要它一直运行。我的代码目前大约有 2000 行,其中一半以上是在找到的单词上运行的,所以我必须对其进行大量简化才能发布它,但我的代码在用户表单中运行这一部分要简单得多。
  • @Kagekiba 我发布的代码背后的概念并没有阻止它在无模式用户窗体中的使用。您只需要适当地设计用户窗体并将我发布的代码中的输入框和消息框引用替换为相关的用户窗体控件引用即可。
【解决方案2】:

通过搜索整个文档(或指定范围)并将每个实例的位置存储在一个数组中,然后您可以将这些位置与当前选择进行比较,并选择当前选择之后的实例。

Function search()

Dim list() As String
Dim Wrd As String
Dim k As Integer
Dim Nfound As Boolean

Dim Def As String
Dim location() As String

'Search document and get locations of each instance of a word

Wrd = "ABC" 'Get next word from list
Def = "Alphabet"
k = 1

Dim DocRng
Set DocRng = ActiveDocument.Content 'search whole document

With DocRng.find
     .Text = Wrd
     .MatchCase = True

    Do While .Execute 'For each entry found, store start and end to array
        ReDim Preserve location(2, k)
        location(1, k) = DocRng.Start
        location(2, k) = DocRng.End
        k = k + 1
    Loop

End With

'Compare the found locations against the current selection and select the first instance after current selection

Nfound = True 'Set as not found until it is found

    j = Selection.End 'mark current cursor location

    For k = 1 To UBound(location, 2)
        If location(1, k) > j + Len(Def) Then '+ Len(Def) accounts for changes to text
            ActiveDocument.Range(Start:=location(1, k), End:=location(2, k)).Select
            Nfound = False
            Exit For
        End If
    Next

    If Nfound Then 'if not found got to first instance found
        k = 1
        ActiveDocument.Range(Start:=location(1, k), End:=location(2, k)).Select
    End If

End Function

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2016-09-25
    • 2023-01-20
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多