How to find the Word files in a directory containing a certain text


I use Windows 8. I have a windows directory with a number of Word-files (.doc -files). Is it possible to search for a certain word, and get in return the doc-files in the directory containing that word?


Who is Participating?
ChrisConnect With a Mentor Commented:
you could try a grep tool which does simple text/string/regex searches

some options   - free - free  - at cost
You can do it with a Word VBA macro like this:
Sub FindWordInDocFilesInFolder()
    Dim strFileName As String
    Dim strFolder As String
    Dim j As Integer
    Dim i As Integer
    Dim strFiles() As String
    Dim doc As Document
    Dim strWord As String
    Dim strMessage As String

    strWord = "the"
    strFolder = "C:\Folder1"
    'process files in current folder
    strFileName = Dir$(strFolder & "\*.doc*")
    Do Until strFileName = ""
        Set doc = Documents.Open(strFolder & "\" & strFileName)
            With doc.Range.Find
                .Text = strWord
                .MatchWholeWord = True
                If .Execute() Then
                    ReDim strFiles(i)
                    strFiles(i) = strFileName
                    i = i + 1
                End If
            End With
        doc.Close wdDoNotSaveChanges
        strFileName = Dir$()
    If i = 0 Then
        strMessage = "There are no document files in " & strFolder & " containing the word " & strWord
        strMessage = "These document files in " & strFolder & " contain the word '" & strWord & "," & vbCrLf
        For j = 0 To i - 1
            strMessage = strMessage & strFiles(j) & vbCrLf
        Next j
    End If
    MsgBox strMessage
End Sub

Open in new window

Shreedhar EtteCommented:
Yes, we can achive this by indexing.

For better understanding please refer this article:,2-221.html
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

[ fanpages ]IT Services ConsultantCommented:


The code statement...

ReDim strFiles(i)

Open in new window

I believe this would need to be...

ReDim Preserve strFiles(i)

Open in new window


adiemeerAuthor Commented:
Thank all for the response. I am not looking for a VB script, but actually want to use the search functionality in the windows explorer. For example selecting a directory and typing *.doc in the search box returns all the files with extension .doc. Now, I am looking for the command that returns not all the .doc files, but only the .doc files that contains a word that I specify.

Hope that my question is clear.
Thanks fanpages.

My quick test returned only one document of two, so I guessed that the word wasn't in the other one.


It looks as if  Windows 8 indexing has improved from the mess that was Windows 7. However I don't have Windows 8. Perhaps shreedhar can be persuaded to expand on the link that (s)he provided.
Shreedhar EtteCommented:

As per the question description you have mentioned Windows 8. Thats why presented you with the link.

Please help me with operating system where you want perfom search.
Note that this isn't my question.

Windows 8 is the OS, as is the page in you link, but the questioner seems to need some further help. I don't have Widows 8, so I can't add anything useful.

Experts-exchange is beginning to discourage relying on links as answers. It does not want to be seen as a link farm (a la BigResource)

There are other problems with external links including that fact that the target page can disappear.
Paul SauvéRetiredCommented:
You can do a search for a phrase by "putting this prhase" in quotation marks. However, if there is more than one file type (format) with the phrase, they will all show up in the search!

EDIT: I don't know if this has been carried over to Windows 8, but in Win7 you can add Type:docx to the search phrase to get only MS Word documents...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.