Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running the same macro on several Word documents automatically 2

Status
Not open for further replies.

almir

Technical User
Apr 18, 2000
46
BA
Hi,

I have a bunch of Word documents that I have to run particular macro on.

This happens on regular basis, i.e. quite often.

What I need is a .VBS that runs the same macro on several Word documents, without having to open each of them individually.

I can put all the documents into separated folder.

Thanks in advance,



 
Greetings almir!

Does this macro exist on all your word documents? If so, then you can use this simple code:
Code:
Dim i, objWord
Dim aryDocs(99)
aryDocs(1) = "C:\doc1"
aryDocs(2) = "C:\doc2"

Set objWord = CreateObject("Word.Application")
For i = 1 to 2
    objWord.Documents.Open(aryDocs(i))
    objWord.Application.Run "Macro1"
    objWord.Quit True
Next
Set objWord = Nothing
If you do not have the same macro within each word document, then I would suggest that you convert you VBA macro to a VBScript and run your new script against your documents.

Good luck!
 
Greetings, snotmare!

You were of great help!

I hope it is not too much if I ask you for just one thing more:

Could you add some code into script that you have already provided? What I need:

It would be perfect if I didn't have to enter names of the files in the script individually, but just a path to the folder containing the files. This means that script would run macro on all the files in that particular folder.

If this is a lot of work for you, forget about it.

Once more: thank you so much!

Al

P. S.

I had to change order of these rows:

objWord.Quit True
Next

to:

Next
objWord.Quit True
.
P. P. S.

I hope my English is good enough!
 
Almir,

Check thread329-1026883
I posted some code here.
If you change the code of the subroutine a little bit, you have a recursive search-subroutine... (filter out the createfolder and copyfile functions)

If you can't get it to work please let me know, and I will assist you...

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Hello Almir!

Thanks for catching my sloppyness, you are correct that the word.quit needs to be outside the loop :).

The file system object makes your needs possible:
Code:
Dim objFileSys, objFile
Set objFileSys = CreateObject("Scripting.FileSystemObject")
For Each objFile in objFileSys.GetFolder("path").Files
    IF UCase(Right(objFile.Path, 4)) = ".DOC" Then
        objWord.Documents.Open(objFile.Path)
        'etc etc
    End If
Next
Set objFileSys = Nothing
'etc etc

If you have sub folders within your main folder, then you will have to do a recursive search like kob3 suggested. There are plenty of examples in these forums that will show you how to do that.

Good luck!
 
Your both were of great help. It works fine for now. For future modificiations I will have to do something myself, not to bother you guys.

Thanks a lot once again!

 
Thnx for the
star.gif

Glad we could help
And don't think you are bothering us with questions. If you have any just ask them.
We all are here to help where/how we can...

Greetz...
K0b3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top