Link to home
Start Free TrialLog in
Avatar of dlewis61
dlewis61Flag for United States of America

asked on

How can I search a word document using more than one term at a time?

Is there a way to search a word document using more than item to search for? I have a long list of numbers I'd like to put in at least 25 at a time, rather than one at a time to find the match in a catalog. What's the most efficient way to do this? The list is in Excel, the search document in Word.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

You could run a VBA macro something like this:
Option Explicit

Sub FindMultiple()
    
    Dim xlApp As Excel.Application
    Dim xlWbk As Excel.Workbook
    Dim xlWks As Excel.Worksheet
    
    Dim wrdRange As Word.Range
    
    Dim r As Integer 'row/record counter
    Dim c As Integer 'column counter
    Dim strNumber As String
    Dim strFolder As String
    Dim strWorkbook As String
    
    
    strFolder = "I:\Allwork\ee\29085446\"
    strWorkbook = "NumberList.xlsx"
    
    'Get Excel application
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    
    
    Set xlWbk = xlApp.Workbooks.Open(filename:=strFolder & strWorkbook, ReadOnly:=True)
    Set xlWks = xlWbk.Sheets(1)
    
    c = 1
    r = 2
    
    strNumber = xlWks.Cells(r, c).Value
    Do While IsNumeric(strNumber)
        Set wrdRange = ActiveDocument.Range
        With wrdRange.Find
            .Text = strNumber
            .MatchWholeWord = True
            Do While .Execute
                MsgBox strNumber & " found on page " & wrdRange.Information(wdActiveEndPageNumber)
            Loop
        End With
        r = r + 1
        strNumber = xlWks.Cells(r, c).Value
    Loop

    'tidy up
    xlWbk.Close False
    xlApp.Quit
    
End Sub

Open in new window

Avatar of dlewis61

ASKER

that's interesting...where is the macro executed? In excel I suppose? where would I insert the name of the word document? I see the workbook, file path, etc. Thanks
It runs in Word, which is why we have to create an Excel application object.
As it stands, the Word document is the active one.
looks like error in find multiples...see attached.
It is a bit tricky to attach files.  The sequence is Attach>Browse>Upload.
I think I can guess the problem. I forgot to tell you to set a reference to the Microsoft Excel Object Library. This is done in the Word VBA editor. From the Menu choose Tools, then References.
Ok. I'm not really good at VBA...can you provide any other solution?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial