在VBA中搜索文档以查找多个术语?

时间:2010-03-17 18:06:35

标签: vba search word-vba excel-vba excel

我正在尝试创建一个在Microsoft Word 2007中使用的宏,该宏将在文档中搜索位于外部Excel文件中的多个关键字(字符串变量)(将其放在外部文件中的原因是这些术语通常会更改和更新)。我已经想出了如何逐段搜索文档中的单个术语并为该术语的每个实例着色,我认为正确的方法是使用动态数组作为搜索术语变量。

问题是:如何让宏创建一个包含外部文件中所有术语的数组,并为每个术语搜索每个段落?

这是我到目前为止所做的:

Sub SearchForMultipleTerms()
'
Dim SearchTerm As String 'declare search term
SearchTerm = InputBox("What are you looking for?") 'prompt for term. this should be removed, as the terms should come from an external XLS file rather than user input.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatti…
With Selection.Find
    .Text = SearchTerm 'find the term!
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
While Selection.Find.Execute
    Selection.GoTo What:=wdGoToBookmark, Name:="\Para" 'select paragraph
    Selection.Font.Color = wdColorGray40 'color paragraph
    Selection.MoveDown Unit:=wdParagraph, Count:=1 'move to next paragraph
Wend

End Sub

感谢您的期待!

2 个答案:

答案 0 :(得分:1)

也许就是这些问题:

Dim cn As Object
Dim rs As Object
Dim strFile, strCon

strFile = "C:\Docs\Words.xls"

'' HDR=Yes, so there are column headings
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

'' The column heading (field name) is Words
strSQL = "SELECT Words FROM [Sheet5$]"
rs.Open strSQL, cn

Do While Not rs.EOF
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = rs!Words '' find the term!
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWholeWord = True
    End With
    While Selection.Find.Execute
        Selection.GoTo What:=wdGoToBookmark, Name:="\Para" 'select paragraph
        Selection.Font.Color = wdColorGray40 'color paragraph
        Selection.MoveDown Unit:=wdParagraph, Count:=1 'move to next paragraph
    Wend

    rs.Movenext
Loop

答案 1 :(得分:0)

嘿,谢谢你的回复!我对你的方法感到有点困惑,我不知道ADODB到底是什么。我实际上最终想出了一个对我有用的方法。对于将来看到这一点的人来说,这里是:

Sub ThisThing()
'

    Dim xlApp As Excel.Application 'defines xlApp to be an Excel application
    Dim xlWB As Excel.Workbook 'defines xlWB to be an Excel workbook
    Set xlApp = CreateObject("Excel.Application") 'starts up Excel
    xlApp.Visible = False 'doesnt show Excel
    Set xlWB = xlApp.Workbooks.Open("P:\SomeFile.xls") 'opens this Excel file

    Dim r As Integer 'defines our row counter, r
    r = 2 'which row to start on

    End With

    With xlWB.Worksheets(1) 'working in Worksheet1
        While xlApp.Cells(r, 1).Formula <> "" 'as long as the cell formula isn't blank

            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
            Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1" 'start at beginning of page
               .Text = xlApp.Cells(r, 1).Formula 'search for the value of cell r
               .Forward = True
               .Wrap = wdFindStop
               .Format = False
               .MatchCase = False
               .MatchWholeWord = False
               .MatchWildcards = False
               .MatchSoundsLike = False
               .MatchAllWordForms = False
               r = r + 1
            End With
            While Selection.Find.Execute
                Selection.GoTo What:=wdGoToBookmark, Name:="\Para"
                Selection.Font.Color = wdColorGray40
                Selection.MoveDown Unit:=wdParagraph, Count:=1
            Wend 'end for the "while find.execute"
        Wend 'end for the "while cells aren't blank"
    End With
    Set wkbBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
End Sub