Run the same macro on multiple files

V

V

I need to run the same macro created in Word (search and replace text macro)
on 500 files in a directory. Is there a way I can tell the macro to go run on
each of those files?
 
J

Jezebel

something along these lines...

Dim pFilename as string
pFilename = dir("c:\....[folder]\*.doc")
Do while len(pFilename) > 0
.... call macro using pFilename

pFilename = dir
Loop
 
H

Helmut Weber

Hi,
not really. You have to open each of the files.
"application.filesearch" e.g. will get you a list of
all fullnames of docs in a folder and put them in
an array. You'll have to loop trough all
the entries in the list. But with 500 files it
might be advisable, not to try to simply open all
and run the macro, but create a kind of log-file
in addition. Let us know, if you need more detailed
advice.
 
J

Jezebel

Jean-Guy reported that, but I have my doubts as to the cause of the problem.
Dir() is a very well- and long-established bit of VB/VBA, going back to
pre-Windows version of Basic. I've never seen a problem with Dir() itself
unless something else was going to screw around with the contents of the
folder while the code was running. And I've never seen a problem if you
simply need to iterate the files in a folder, as is the case here.
 
H

Helmut Weber

Hi Jezebel,
it was just in pre-Windows times, that I notized
this. Then, it was Word's handling of the actual
doc, the temporary doc and the backup doc at the
same time, possibly in the same directory or on
the same floppy, of course. When editing a file,
lets say the first file in a list, as reported by
Norton "dirlist" (I think it was), after saving and
closing, it appeared at the end of the list, and
thus was processed a second time, when working
towards the list's end. But may well be, that this
doesn't apply anymore and I am wrong.
Have a nice day.
 
H

Helmut Weber

Hi Jezebel,
should have tested it before, sorry.
Can't find anything wrong with dir().
Not my day.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
J

Jezebel

Actually, your reference to Word's temporary files might touch on a real
problem. As I mentioned, if you muck around with what's actually in the
folder from within the Dir loop, presumably you can cause a problem
(although I've never met one, and I've written plenty of macros that do
exactly as this one does). Eg, if you used Dir to retrieve ALL files from
the folder (not just *.doc) you might conceivably get a file twice: once for
the .doc and again for the .wbk.

I guess if you're really worried about it you could use the Dir loop to
collect the file names into an array or a collection, then iterate that as a
separate operation.
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Helmut Weber > écrivait :
In this message, < Helmut Weber > wrote:

|| Hi Jezebel,
|| using dir() and processing the returned files
|| you may find, that in some cases a file is processed
|| more than once, as Jean-Guy Marcil mentioned. See:
||
http://groups.google.com/groups?hl=de&lr=&ie=UTF-8&threadm=uQq7c$uWEHA.3716%
|| 40TK2MSFTNGP11.phx.gbl&rnum=1&prev=/groups%3Fas_q%3Ddir%
|| 26ie%3DUTF-8%26as_ugroup%3Dmicrosoft.public.word.vba*%
|| 26as_uauthors%3DJean-Guy%2520%26lr%3D%26hl%3Dde
|| ---
|| Thread is "saving files as pdf" by Chad Demeyer.

In fact, I was copying the content of a folder to a new one, then opening
each file in turn to generate a pdf. In this particular case, as you and
Jezebel suggested, I had the problem because I as mucking with the folder's
content.

By the way, Helmut, in case you did not know this resource, you may want to
check out
http://tinyurl.com/
so that the monstrous URL above becomes:
http://tinyurl.com/5jo6z

Cheers!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Jean-Guy,
great idea that is.
I'll promote it.
---
Salut!
Greetings from Bavaria, a european region
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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