Finding a word and displaying results

F

ffn

Hi there,

need help with how to go about writing VBA (or VB Script) that will look
into a directory for all .doc Word documents and see if the content of each
..doc file contains a certain keyword. For all .doc files that has content
that contains the keyword, display the files in a nice list.
Example usage: list all the .doc Word documents in C:\A\B that contains the
word "cat".

Any suggestions, directions, code much appreciated!
Thanks!
 
D

Doug Robbins

Modify the procedure in the article "Find & ReplaceAll on a batch of
documents in the same folder" at:

http://word.mvps.org/FAQs/MacrosVBA/BatchFR.htm


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
F

ffn

Thanks Doug,

that certainly helped. I have now modified the code to find a particular
text in the content of the file. Is there anyway that I can execute the
"find" and hide the window that is used to open each .doc file in the
directory to perform the "find"?
Right now, a window is opened and then closed each time it performs the
"find" as it iterates through the files.

Also, can someone direct me to where I can find out about displaying the
list of "found" files so that the user can click on a found file and open up
the document? Right now, I'm just returning the filename in a msgBox.

Cheers and thanks again.
 
D

Doug Robbins

To get the filenames into a document, do something like this.

At the beginning the procedure, add

Dim target as Document
Set target = Documents.Add

Then at the point where you presently have the MsgBox, use

target.Range.InsertAfter [what you now have in the MsgBox command] & vbCr

Then, after the procedure has run, the target document should contain a list
of the files that contain the text.

If you then run a macro containing the following code, it should convert
each (full) filename into a hyperlink.

Dim i As Long, frange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
Set frange = ActiveDocument.Paragraphs(i).Range
frange.End = frange.End - 1
ActiveDocument.Hyperlinks.Add Anchor:=frange, _
Address:=frange.Text
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
F

ffn

Hi Doug,

thanks again for very helpful tips. I was wondering if there is a way to
insert the found files (actually if they could be inserted as links as you
suggested, that's best) into a pop-up window rather than a new document.

Cheers.

Doug Robbins said:
To get the filenames into a document, do something like this.

At the beginning the procedure, add

Dim target as Document
Set target = Documents.Add

Then at the point where you presently have the MsgBox, use

target.Range.InsertAfter [what you now have in the MsgBox command] & vbCr

Then, after the procedure has run, the target document should contain a
list of the files that contain the text.

If you then run a macro containing the following code, it should convert
each (full) filename into a hyperlink.

Dim i As Long, frange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
Set frange = ActiveDocument.Paragraphs(i).Range
frange.End = frange.End - 1
ActiveDocument.Hyperlinks.Add Anchor:=frange, _
Address:=frange.Text
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a
paid consulting basis.

Hope this helps,
Doug Robbins - Word MVP
ffn said:
Thanks Doug,

that certainly helped. I have now modified the code to find a particular
text in the content of the file. Is there anyway that I can execute the
"find" and hide the window that is used to open each .doc file in the
directory to perform the "find"?
Right now, a window is opened and then closed each time it performs the
"find" as it iterates through the files.

Also, can someone direct me to where I can find out about displaying the
list of "found" files so that the user can click on a found file and open
up the document? Right now, I'm just returning the filename in a msgBox.

Cheers and thanks again.
 
D

Doug Robbins

I am not sure what you mean by a pop-up window. However, you could populate
a listbox on a userform with the found files and have code in the useform
that would open a document that the user clicked on in that listbox.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
ffn said:
Hi Doug,

thanks again for very helpful tips. I was wondering if there is a way to
insert the found files (actually if they could be inserted as links as you
suggested, that's best) into a pop-up window rather than a new document.

Cheers.

Doug Robbins said:
To get the filenames into a document, do something like this.

At the beginning the procedure, add

Dim target as Document
Set target = Documents.Add

Then at the point where you presently have the MsgBox, use

target.Range.InsertAfter [what you now have in the MsgBox command] & vbCr

Then, after the procedure has run, the target document should contain a
list of the files that contain the text.

If you then run a macro containing the following code, it should convert
each (full) filename into a hyperlink.

Dim i As Long, frange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
Set frange = ActiveDocument.Paragraphs(i).Range
frange.End = frange.End - 1
ActiveDocument.Hyperlinks.Add Anchor:=frange, _
Address:=frange.Text
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a
paid consulting basis.

Hope this helps,
Doug Robbins - Word MVP
ffn said:
Thanks Doug,

that certainly helped. I have now modified the code to find a particular
text in the content of the file. Is there anyway that I can execute the
"find" and hide the window that is used to open each .doc file in the
directory to perform the "find"?
Right now, a window is opened and then closed each time it performs the
"find" as it iterates through the files.

Also, can someone direct me to where I can find out about displaying the
list of "found" files so that the user can click on a found file and
open up the document? Right now, I'm just returning the filename in a
msgBox.

Cheers and thanks again.

Modify the procedure in the article "Find & ReplaceAll on a batch of
documents in the same folder" at:

http://word.mvps.org/FAQs/MacrosVBA/BatchFR.htm


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a
paid consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
F

ffn

Sorry, I may be using incorrect terminology - a userform may be just what
I'm looking for...I'm quite new to VBA (2 1/2 weeks now)!

Basically, I would like the Macro to place the found files in some other
persistent window (form?) that the user can switch between that window/form
and the document he is working on.
In the result window/form, the user can click on any of the found files and
it would open up in Word.
Any files that are found are placed in the result window/form.

I'll try to read up on userforms...do you know of any particular tutorials
that are most relevant to my specifications?

Cheers and thanks.
 
D

Doug Robbins

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

to get started, but that is only a very simple example of what can be done.
When you find each file, you will use the .AddItem method of the listbox to
add it to a listbox.

You will need to dig into the help in the Visual Basic Editor to get up to
speed with all that you will need. See how you go and post back here when
you get stumped. The best way to learn however is to try and do it
yourself.
--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top