Open all docs in folder

M

Mark

i am using €office 97 upwards.

Cansomeone asist me with some code which looks in a particular folder, opens
each document one by one and does something then saves it and closes it and
open next document.

Open each doc in folder 1 by 1

with active doc

do something

save document

close document

next document
 
J

Jezebel

Dim pFileName as string
Dim pDoc as Word.Document

pFileName = Dir("C:\MyFolder\*.doc")
Do until len(pFileName) = 0
set pDoc = Documents.Open(pFileName)
... do whatever with pDoc
pDoc.Close SaveChanges:=TRUE
pFileName = Dir
Loop
 
M

Mark

Thanks,

i have tried this and altered the folder path to suit, it finds the first
file and then debugs on the 'set pDoc = Documents.Open(pFileName)' line with
a run-time rror 5174 'this file could not be found'.

Try one of the following
Check spelling of the document
Try a different filename

Equally when I change the path to 'R' drive (a network drive) it doesn't
find anything and end subs.

Any ideas?
 
D

Doug Robbins - Word MVP

You need to use:

Dim pFileName As String
Dim pDoc As Word.Document

pFileName = Dir("C:\MyFolder\*.doc")
Do Until Len(pFileName) = 0
MsgBox pFileName
Set pDoc = Documents.Open("C:\MyFolder\" & pFileName)
pFileName = Dir
Loop


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Top