Find Next across Multiple Files Macro

A

anotherjosh

Any macro gurus out there? What I'm hoping to accomplish is to have a
simple macro that takes a directory and recurses through each
subdirectory looking for the occurrences of a specific word in each
Word document. It should basically have the Find Next functionality
except that when it hits the end of one file, it proceeds to the next.
I have written a macro that searches through all files and reports back
all files that contain a given string. However, that functionality can
be obtained by just hitting Start -> Search -> Files and Folders...

Any suggestions? Thanks in advance!
Josh
 
M

Mark Tangard

Hi Josh,
Any macro gurus out there?

Um, well, this is the VBA general newsgroup, where macro gurus
are sort of like wallpaper.
However, that functionality can be obtained by just hitting
Start -> Search -> Files and Folders...

Correct. So why did you want a macro to do the same thing?
Or have I missed something in your description? Did you want
to do something more once the occurrences are found -- replace
them? Highlight them? Produce a summary?

(I've built macros to do all 3 of the above -- a necessity
since we work constantly with multiple huge files comprising
one "document." Never was able accomplish subdir recursion
though; they work only within a given folder.)
 
A

anotherjosh

I have the sub-directory recursion working. I can't figure out how to
mimic the find next functionality, though. Ultimately, here's what I'm
aiming for:

1. The user enters a search string. (working)
2. The macro starts opening word documents as it finds them.
(working)
3. Once it finds an occurrence of the word, it drops the cursor at the
point where the word occurs and asks the user if they want to continue
searching. (not working)
4. Once the macro hits the end of a file, it continues searching to
the next file. (working)

Sorry if I'm not being very clear. Too much coffee and too little
sleep. Thanks!
 
M

Mark Tangard

Josh,

Ah. Well, stopping-and-querying isn't part of my batch-find
macros. Without seeing the code I hesitate to tell you to
paste something into it, but chances are all you need is a
Do While loop inside your Find block (warning: untested code):

With Selection.Find
.Text = "dsffasdf"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
:
: and so on
:
Do While .Execute
If MsgBox("More?", vbYesNo) = vbNo Then
GoTo HeWantsMore
Else
GoTo QuitThisDoc
End If
HeWantsMore:
Loop
End With

And put the QuitThisDoc label just before the line that closes
the current document.

The "Do While .Execute" tells VBA to pop up the MsgBox whenever
..Execute returns true (i.e., the text is found). It's a very
convenient trick.

[If this works, howbout explaining how you got it to recurse!?]
 
A

anotherjosh

Thank you! I think that is going to work perfect.

As for the recursing, I wish I could claim I wrote it myself. I found
this nifty little macro at http://www.funduc.com/word_sr.htm. It has
the recursion built in. If you fire up the macro editor, right click
on the form and view the source code. It shows how they are recursing.


I have other code that recurses (that I actually wrote), but it isn't
as elegant as the macro found at the URL above.

Let me know if you have any problems with it. Thanks again for your
help on this.

-Josh
 

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