loop through all docs in a folder

R

red6000

Hi, how do I loop through all documents in a folder and perform a task?

ie what I would like to do is:

- while there is a word document in folder
- open word document
- perform code
- next word document if there is one

Thanks.
 
H

Helmut Weber

Hi red6000,

a million variations available in the world wide net,
like this, just another one:

Sub Something()
Dim cCol As Collection
Dim sStr As String
Dim lCnt As Long
Set cCol = New Collection
sStr = Dir("c:\test\word\*.doc", vbNormal)
While sStr <> ""
cCol.Add sStr
sStr = Dir
Wend

For lCnt = 1 To cCol.Count
MsgBox cCol.Item(lCnt)
Documents.Open cCol.Item(lCnt)
' your code, beware of what you are doing
ActiveDocument.Close
Next

End Sub
- while there is a word document in folder

Is that the condition you really want?

The example leaves the documents in the acual folder.
So "while there is a word document in folder"
will never be false.
You got to move the files to another location,
that is copy them, kill the source.

?

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
P

Perry

here's a link with all info regarding files/folders.
Try to implement in yr application and if you get stuck, repost indicating
which version of
MS Word you are using and relevant or failing code parts
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarofftalk/html/office09072000.asp

Check below function in VBA help as well.
Dir()

And here's a good example:
http://word.mvps.org/faqs/macrosvba/BatchFR.htm

Good luck

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 

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