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.

Re: loop through all docs in a folder by Helmut

Helmut
Sat May 19 13:50:18 CDT 2007

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"


Re: loop through all docs in a folder by Perry

Perry
Sat May 19 13:50:33 CDT 2007

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



"red6000" <red1000002001@yahoo.com> schreef in bericht
news:464f2859$0$3838$c3e8da3@news.astraweb.com...
> 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.
>