Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Run the same macro on multiple files

17 views
Skip to first unread message

Jezebel

unread,
Aug 12, 2004, 3:40:33 AM8/12/04
to
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

"V" <V...@discussions.microsoft.com> wrote in message
news:A85D3D48-17B7-4A51...@microsoft.com...
> 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?


Helmut Weber

unread,
Aug 12, 2004, 3:50:27 AM8/12/04
to
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.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000

Helmut Weber

unread,
Aug 12, 2004, 3:56:54 AM8/12/04
to
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%24uWEHA.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.

Jezebel

unread,
Aug 12, 2004, 4:39:19 AM8/12/04
to
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.

"Helmut Weber" <anon...@discussions.microsoft.com> wrote in message
news:4cec01c48041$ef0db8e0$a401...@phx.gbl...

Helmut Weber

unread,
Aug 12, 2004, 5:07:52 AM8/12/04
to
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.

Helmut Weber

unread,
Aug 12, 2004, 5:46:36 AM8/12/04
to
Hi Jezebel,
should have tested it before, sorry.
Can't find anything wrong with dir().
Not my day.

Jezebel

unread,
Aug 12, 2004, 7:44:31 AM8/12/04
to
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.


"Helmut Weber" <anon...@discussions.microsoft.com> wrote in message

news:4da701c48051$424e23a0$a601...@phx.gbl...

Jean-Guy Marcil

unread,
Aug 12, 2004, 10:16:57 AM8/12/04
to
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%24uWEHA.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
jmarci...@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Helmut Weber

unread,
Aug 12, 2004, 10:47:25 AM8/12/04
to
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

0 new messages