Run Macro on Multiple Files in a Directory

R

Rick Robinson

I have a macro which extracts particular data from a .rtf file. I
want to run this on many files in a directory automatically. I just
cant seem to identify the correct looping code to do this.

What simple code would execute these instructions on multiple files in
the same directory?

Thanks in Advance for any assistance!

Rick
 
A

Andrew Savikas

Here's a very simple loop that'll work on all the files in
a folder. Wouldn't take much to test for rtf-only files.
(Most of this code is from someone else on this board,
from quite some time ago, sorry I can't credit more
specifically).

Sub DoSomethingToAllFilesInDirectory()
Dim fs As FileSearch
Dim i As Integer
Dim mydoc As Document
Set fs = Application.FileSearch
With fs
.LookIn = "C:\My\Folder\Location"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
Set mydoc = Documents.Open(.FoundFiles(i))
'Do whatever you want to mydoc here
Next i
Else
MsgBox "No files in selected folder"
End If
End With
End Sub
 

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