String 使用vba office word查找由一个单词分隔的文本中的匹配字符串(甚至字符串)

String 使用vba office word查找由一个单词分隔的文本中的匹配字符串(甚至字符串),string,vba,search,ms-word,find,String,Vba,Search,Ms Word,Find,在这个问题上我需要你的帮助。我正在尝试在Office Word中编写一个VBA宏,用于搜索文档文本中定义的字符串。这对我来说是直截了当的。 现在的特殊性是如果文本中的某些字符串由一个单词分隔,则进行匹配 下面是在文档中查找匹配字符串的基本代码,但我正在努力找出如何在一个单词分隔的字符串上进行匹配 Sub HighlightMatches() Dim range As range Dim i As Long Dim wordsArray wordsArray = Arra

在这个问题上我需要你的帮助。我正在尝试在Office Word中编写一个VBA宏,用于搜索文档文本中定义的字符串。这对我来说是直截了当的。 现在的特殊性是如果文本中的某些字符串由一个单词分隔,则进行匹配

下面是在文档中查找匹配字符串的基本代码,但我正在努力找出如何在一个单词分隔的字符串上进行匹配

Sub HighlightMatches()
    Dim range As range
    Dim i As Long
    Dim wordsArray

wordsArray = Array("Lion", "Hello", "Cat", "Lorem Ipsum")
For i = 0 To UBound(wordsArray)
    Set range = ActiveDocument.range

    With range.Find
    .Text = wordsArray(i)
    .Format = True
    .MatchCase = False
        Do While .Execute(Forward:=True) = True
            range.HighlightColorIndex = wdYellow
        Loop
    End With

Next
End Sub
我试图实现的目标:如果在文档文本中有一句话像“Lorem is Ipsum,仅此而已”;我希望搜索能够突出显示“Lorem is Ipsum”,即使单词sarray中没有“Lorem is Ipsum”


如果你们能帮我,我会很感激的。提前感谢您的时间。

用单词分隔的字符串很容易找到-尝试拆分字符串,然后计算数组中的单位。一般来说,假设你可以轻松做到这一点,并总结你的问题,当我有两个单词,中间有一个随机单词时,找到一个匹配项,这将非常符合你的需要:

Option Explicit

Public Sub TestMe()

    Dim strText         As String
    Dim arrMatches      As Variant

    Dim arrIn        As Variant
    Dim myArr1          As Variant
    Dim myArr2          As Variant
    Dim lngC            As Long

    arrMatches = Array(Array("lorem", "ipsum"), Array("of", "the"))

    strText = "lorem my ipsum is simply dummy text of fu the printing " & _
                    "and typesetting industry."

    arrIn = Split(strText)

    'we need to check always 2 less:
    For lngC = LBound(arrIn) To UBound(arrIn) - 2
        For Each myArr1 In arrMatches
            If myArr1(0) = arrIn(lngC) And myArr1(1) = arrIn(lngC + 2) Then
                Debug.Print arrIn(lngC) & " " & arrIn(lngC + 1) & " " & arrIn(lngC + 2)
            End If
        Next myArr1
    Next lngC

End Sub
可能很难理解它的功能,因此需要将
arrMatches
更改几次,并将
strText
也更改几次。然后用
F8
进行调试。在这种情况下,它会在即时窗口中返回以下内容:

lorem my ipsum
of fu the
这是因为我们有两个arrmaches
Array(Array(“lorem”、“ipsum”)、Array(“of”、“the”))
,文本中只有一个字符串,这些匹配之间有一个单词,就是上面的字符串

编辑:如果要使其在两个单词之间工作超过1个单词,请按如下方式选中:

Option Explicit
Public Sub TestMe()

    Dim strText         As String
    Dim strPrint        As String
    Dim arrMatches      As Variant
    Dim arrInput        As Variant
    Dim myArr1          As Variant
    Dim myArr2          As Variant
    Dim lngC            As Long
    Dim lngC2           As Long
    Dim lngC3           As Long

    arrMatches = Array(Array("lorem", "ipsum"), Array("of", "the"))
    strText = "lorem my ipsum is simply dummy text of fu the printing " & _
               "ipsum and typesetting industry."

    arrInput = Split(strText)
    For lngC = LBound(arrInput) To UBound(arrInput)
        For Each myArr1 In arrMatches
            For lngC2 = lngC To UBound(arrInput)
                If myArr1(0) = arrInput(lngC) And myArr1(1) = arrInput(lngC2) Then
                    strPrint = ""
                    For lngC3 = lngC To lngC2
                        strPrint = strPrint & " " & arrInput(lngC3)
                    Next lngC3
                    Debug.Print strPrint
                End If
            Next lngC2
        Next myArr1
    Next lngC
End Sub

用单词分隔的字符串很容易找到-尝试拆分字符串,然后计算数组中的单位。一般来说,假设你可以轻松做到这一点,并总结你的问题,当我有两个单词,中间有一个随机单词时,找到一个匹配项,这将非常符合你的需要:

Option Explicit

Public Sub TestMe()

    Dim strText         As String
    Dim arrMatches      As Variant

    Dim arrIn        As Variant
    Dim myArr1          As Variant
    Dim myArr2          As Variant
    Dim lngC            As Long

    arrMatches = Array(Array("lorem", "ipsum"), Array("of", "the"))

    strText = "lorem my ipsum is simply dummy text of fu the printing " & _
                    "and typesetting industry."

    arrIn = Split(strText)

    'we need to check always 2 less:
    For lngC = LBound(arrIn) To UBound(arrIn) - 2
        For Each myArr1 In arrMatches
            If myArr1(0) = arrIn(lngC) And myArr1(1) = arrIn(lngC + 2) Then
                Debug.Print arrIn(lngC) & " " & arrIn(lngC + 1) & " " & arrIn(lngC + 2)
            End If
        Next myArr1
    Next lngC

End Sub
可能很难理解它的功能,因此需要将
arrMatches
更改几次,并将
strText
也更改几次。然后用
F8
进行调试。在这种情况下,它会在即时窗口中返回以下内容:

lorem my ipsum
of fu the
这是因为我们有两个arrmaches
Array(Array(“lorem”、“ipsum”)、Array(“of”、“the”))
,文本中只有一个字符串,这些匹配之间有一个单词,就是上面的字符串

编辑:如果要使其在两个单词之间工作超过1个单词,请按如下方式选中:

Option Explicit
Public Sub TestMe()

    Dim strText         As String
    Dim strPrint        As String
    Dim arrMatches      As Variant
    Dim arrInput        As Variant
    Dim myArr1          As Variant
    Dim myArr2          As Variant
    Dim lngC            As Long
    Dim lngC2           As Long
    Dim lngC3           As Long

    arrMatches = Array(Array("lorem", "ipsum"), Array("of", "the"))
    strText = "lorem my ipsum is simply dummy text of fu the printing " & _
               "ipsum and typesetting industry."

    arrInput = Split(strText)
    For lngC = LBound(arrInput) To UBound(arrInput)
        For Each myArr1 In arrMatches
            For lngC2 = lngC To UBound(arrInput)
                If myArr1(0) = arrInput(lngC) And myArr1(1) = arrInput(lngC2) Then
                    strPrint = ""
                    For lngC3 = lngC To lngC2
                        strPrint = strPrint & " " & arrInput(lngC3)
                    Next lngC3
                    Debug.Print strPrint
                End If
            Next lngC2
        Next myArr1
    Next lngC
End Sub

这是我所说的问题的解决办法。它比@vityta提供的解决方案更具体、更具可扩展性,但这很有帮助,因为我从中了解了一些逻辑

Sub HighlightMatches()
    Dim phraseRange As Object
    Dim phraseIndex As Long
    Dim i As Long
    Dim wordsArray() As Variant
    Dim splitedList As Variant
    Dim keywords As String
wordsArray = Array("Faux Texte", "Lorem Ipsum")
For i = 0 To UBound(wordsArray)
    Set phaseRange = ActiveDocument.Sentences
    splitedList = Split(Trim(wordsArray(i)))
    If UBound(splitedList) > 0 Then
        For phraseIndex = 1 To ActiveDocument.Sentences.Count
            keywords = ""
            Set phraseRange = ActiveDocument.Sentences(phraseIndex)
            With phraseRange.Find
                For t = 0 To UBound(splitedList)
                    keywords = keywords + "(" & splitedList(t) & ")(*)"
                Next
                'MsgBox keywords
                .Text = keywords
                .Format = True
                .MatchWildcards = True
                    If .Execute(Forward:=True) = True Then
                        'MsgBox "It Works"
                        phraseRange.HighlightColorIndex = wdYellow
                    End If
            End With
        Next
    End If
Next
End Sub

就这些。我希望这能为其他人节省时间。

这是我所说的解决问题的方法。它比@vityta提供的解决方案更具体、更具可扩展性,但这很有帮助,因为我从中了解了一些逻辑

Sub HighlightMatches()
    Dim phraseRange As Object
    Dim phraseIndex As Long
    Dim i As Long
    Dim wordsArray() As Variant
    Dim splitedList As Variant
    Dim keywords As String
wordsArray = Array("Faux Texte", "Lorem Ipsum")
For i = 0 To UBound(wordsArray)
    Set phaseRange = ActiveDocument.Sentences
    splitedList = Split(Trim(wordsArray(i)))
    If UBound(splitedList) > 0 Then
        For phraseIndex = 1 To ActiveDocument.Sentences.Count
            keywords = ""
            Set phraseRange = ActiveDocument.Sentences(phraseIndex)
            With phraseRange.Find
                For t = 0 To UBound(splitedList)
                    keywords = keywords + "(" & splitedList(t) & ")(*)"
                Next
                'MsgBox keywords
                .Text = keywords
                .Format = True
                .MatchWildcards = True
                    If .Execute(Forward:=True) = True Then
                        'MsgBox "It Works"
                        phraseRange.HighlightColorIndex = wdYellow
                    End If
            End With
        Next
    End If
Next
End Sub

就这些。我希望这能为其他人节省时间。

谢谢@vityta这很有效。现在我要做的是尝试以正确的方式在代码中插入它。再次感谢您抽出时间。我在问自己,如果我们能找到并突出句子中的单词,即使有多个单词分隔,代码会变成什么样子!?你觉得你能帮上忙吗?太棒了@vityta你太棒了!谢谢你的时间和反应:D。我会继续。谢谢你@vityta,这很有效。现在我要做的是尝试以正确的方式在代码中插入它。再次感谢您抽出时间。我在问自己,如果我们能找到并突出句子中的单词,即使有多个单词分隔,代码会变成什么样子!?你觉得你能帮上忙吗?太棒了@vityta你太棒了!谢谢你的时间和反应:D。我会继续的。