Add.found=划定字符串VBA的真值

Add.found=划定字符串VBA的真值,vba,ms-word,Vba,Ms Word,我正在使用find函数在word文档中搜索值。当找到这些值时,我试图将它们放在描述的字符串中,该字符串稍后将在脚本中使用 我能够找到这些值,但当我找到时,脚本会将搜索参数中的值添加到字符串中。我只需要添加文档中实际存在的值 Sub FilePick() Dim propstr As String Dim wordcollection(10) As String Dim words As Variant wordcollection(0) = "PJ" wordcollection(

我正在使用find函数在word文档中搜索值。当找到这些值时,我试图将它们放在描述的字符串中,该字符串稍后将在脚本中使用

我能够找到这些值,但当我找到时,脚本会将搜索参数中的值添加到字符串中。我只需要添加文档中实际存在的值

Sub FilePick()


Dim propstr As String
 Dim wordcollection(10) As String
    Dim words As Variant
wordcollection(0) = "PJ"
wordcollection(1) = "E1233"
wordcollection(2) = "E048"
wordcollection(3) = "E144"
wordcollection(4) = "E849"
wordcollection(5) = "E977"
wordcollection(6) = "IL0021"
wordcollection(7) = "MISC001"
wordcollection(8) = "CG0001"
wordcollection(9) = "CG2107"
wordcollection(10) = "Blah"
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
For Each words In wordcollection
With Selection.Find
.Text = words

 .Forward = True
                .Wrap = wdFindContinue
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False

If .Found = True Then
propstr = propstr & " " & words
             End If
            Selection.Find.Execute
任何帮助都将不胜感激


谢谢

使用
LBound
UBound
迭代
wordcollection
数组的所有元素;另外,移动
。在
With…End With
块中执行
,然后检查
是否找到

Sub FilePick()
    Dim propstr As String
    Dim wordcollection(10) As String
    Dim i As Integer

    wordcollection(0) = "PJ"
    wordcollection(1) = "E1233"
    wordcollection(2) = "E048"
    wordcollection(3) = "E144"
    wordcollection(4) = "E849"
    wordcollection(5) = "E977"
    wordcollection(6) = "IL0021"
    wordcollection(7) = "MISC001"
    wordcollection(8) = "CG0001"
    wordcollection(9) = "CG2107"
    wordcollection(10) = "Blah"

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting

    For i = LBound(wordcollection) To UBound(wordcollection)
        With Selection.Find
            .Text = wordcollection(i)
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute

            If .Found Then
                propstr = propstr & " " & wordcollection(i)
            End If
        End With
    Next i

    Debug.Print "Result: " & propstr
End Sub

普罗蒂普:谢谢!这让我在这个项目上走得更远了。