【问题标题】:Searching a document for multiple terms in VBA?在 VBA 中搜索多个术语的文档?
【发布时间】:2010-03-19 07:00:53
【问题描述】:

我正在尝试创建一个在 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

感谢收看!

【问题讨论】:

    标签: vba search ms-word excel


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您好,谢谢您的回复!我对你的方法有点困惑,我不知道 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-03
        • 2018-10-01
        • 1970-01-01
        • 2018-08-09
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多