Log in

View Full Version : Directory Search for Body Text in Word 2007



ksiva8
10-17-2011, 08:24 AM
Please please help: I am now infuriated and brain-tired of searching the web resource for this supposed simple code:

Can someone point me to a code which searches a given file directory by using a word search (like Window Explorer's Search)?

Also, I understand that there is a way to use keyword search to access the Document Properties of a Word file without opening the document - sound like optimal method perhaps.

I am stuck because 'Filesearch' was removed by MSoft, so following code no longer works: Thanks in antiicipation, VBAX newbie


Tag ...dim vsearch as string
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = "C:\My Documents\SivaAM_Portable\News"
.TextOrProperty = vSearch
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles

If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
If .FoundFiles.Count <> 0 Then
Documents.Add
ActiveDocument.SaveAs (vSearch)
Documents.Open (vSearch)
End If....

ksiva8
10-18-2011, 01:38 AM
Sometimes the answer to the solution is in how the problem is posed, as Bertrand Russell, the uber philosopher stated.

I am simply looking for a VBA code to List all files in directory containing key word(s), similar to the Windows Explorer function. any Help?

ksiva8
10-18-2011, 04:33 PM
...bump. please?

steve_flash
10-20-2011, 08:21 AM
have you tried this? It's from technet. Instead of Find and Replace use Find and then List

Const wdReplaceAll = 2
Set oWord = CreateObject("Word.Application")
oWord.Visible = False
Set oDoc = oWord.Documents.Open("d:\temp\Test.doc")
With oWord.Selection
.Find.Text = "Flat"
.Find.Replacement.Text = "Appartment"
.Find.Forward = True
.Find.MatchWholeWord = True
.Find.Execute ,,,,,,,,,,wdReplaceAll
End With
oDoc.Save
oDoc.Close
oWord.Quit

ksiva8
10-20-2011, 10:17 AM
Firstly, thank you Steve for coming to the rescue...

I am seeking a code to search an entire file Directory by key word(s) in VBA for Word (maybe via Excel platform!?).

I think you suggestion was to search open documents. Previously without even opening a file the 'Filesearch' function allowed keyword searches, like Window Explorer.
Hey, your efforts appreciated. ;-) but Still scratching my head!!

ksiva8
11-16-2011, 07:30 AM
:mkay Anyone outthere with the replacement solution to 'Filesearch' function please. I stubbornly think its there, and have seen various alternative codes, but none that does not have to open each file to search for the text.

Dave
11-18-2011, 07:20 AM
Use Dir to replace the filesearch function. A google should find you some code. HTH. Davr

ksiva8
11-22-2011, 07:08 PM
Thanks Dave. I took your advice but all I did was wandering till I got the same answer that you have to open a file to search for particular text. I want to search a directory for keywords WITHOUT opening the file; Filesearch did that, Dir does not as I understand! Thanks Though.

Any more suggestions / help please?

Tinbendr
11-23-2011, 07:22 AM
There are several examples of other code replacements, but they only list the files in the directory. I did find one for $$$ (http://www.codematic.net/excel-tools/office-2007-filesearch.htm), but I also found this one (http://dfenton.com/DFA/download/Access/FileSearch.html).

Hope this helps.