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 strongm 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 1

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,
 
Since I know nothing about VBS, but something about VBA, I'm just curious:

Why not use a GetOpenFileName with multiple files enabled? That way you can get the files you want without any other definitions, and open them programatically, something like this

Code:
Dim myFiles As Variant
Dim N As Long

myFiles = Application.GetOpenFilename( _
        FileFilter:="Word documents (*.xls), *.xls", _
        FilterIndex:=1, _
        MultiSelect:=True)

If IsArray(myFiles) Then

For N = LBound(myFiles) To UBound(myFiles)
        Workbooks.Open myFiles(N)
        Call YOURMACRO
        workbooks.close myFiles(N) savechanges:=True   
Next
End If


// Patrik
______________________________

To the optimist, the glass is half full. To the pessimist, the glass is half empty. To the IT Professional, the glass is twice as big as it needs to be.
 
Hi 01Patrik,

If you read the post again, it specified Word - not Excel. Your suggested code uses Excel extensions. What is with you Excel guys. Excel, Excel...everything is always Excel, even when it clearly states Word. Sheeesh...just kidding.

Oh, and the post clearly states that the files are to be acted on WITHOUT opening them - your codes and your post uses code to open the...ahem, Excel files.

almir,

It is possible to act on files without opening them, but such operations are pretty much limited to file attributes. If your macro does any format, any text retrieval, text input, actions involving bookmarks or fields - in other words any actions on the document CONTENTS, then the file has to be opened.

Gerry
 
Patrick,

Thank you very much.

Gerry,

You are right, but it was my mistake: of course I have to open them, but I do not want to open each of them one by one and then run macro on them.

Patrick,

Could you send me a code above, but for Word documents.

Thanks both of you.

Al
 
So you think you are going to open ALL of your files at the same time and do your operations on them. Good luck. You are still going to have to action each one individually.

If I may make a suggestion - as i do not know what it is you are trying to action on the documents - perform whatever it is through the Documents collection.

Gerry
 
I found a solution in other way.

Anyway, thanks again to all of you!

Al
 
Hey! That is not the way things work here. If you find a solution, it is good manners to share. Please post what you came up with. It may help others.

Thanks.

Gerry
 
...I was in a bit of stress posting the other day.

Of course I meant "documents.open" instead of "workbooks.open", and "*.doc" instead of "*.xls".

The rest should be the same, was the idea.

Gerry:

What's up with you? Why so aggressive? I was proposing a solution to the problem - I guessed (correctly, it seems) that the problem was opening the files manually and running the macro on each of them, NOT the files being open.

Perhaps you are right in your suggestions, I'm not sure - but man, your tone makes me want to ignore all your messages (if that's possible). Now, I don't want to start a pis*ing contest, I simply mean that insulting and stupifying people isn't the best way to make people A/ ask questions if there unsure, and B/ post the solutions if found elsewhere.

I agree this isn't a helpdesk, and people should show their appriciation - but good mannners goes both ways. Anyway, I hope you just had a bad day, as I've seen and appriciated your solutions elsewhere.

Cheers /Patrik


// Patrik
______________________________

To the optimist, the glass is half full. To the pessimist, the glass is half empty. To the IT Professional, the glass is twice as big as it needs to be.
 
Hi guys,

Sorry for my misbehaviour, but I am new here. Also, English is not my mother language.

Anyway, solution that I have found is like this:

***************
Dim i, objWord
Dim aryDocs(99)
aryDocs(1) = "c:\doc1"
aryDocs(2) = "c:\doc2"
aryDocs(3) = "c:\doc3"

' making an array of documents with their paths

Set objWord = CreateObject("Word.Application")

' creating Word app

For i = 1 to 3

' definition of files from array

objWord.Documents.Open(aryDocs(i))
objWord.Application.Run "MyMacro"

'running my macro

Next
' looping

objWord.Quit True

' quiting the app

Set objWord = Nothing

'end

****************

Once again, thanks both of you, and please respect my apologize.

Regards,

Al
 
No need to apologize. Glad it worked out for you.

// Patrik
______________________________

To the optimist, the glass is half full. To the pessimist, the glass is half empty. To the IT Professional, the glass is twice as big as it needs to be.
 
Patrik....thank you for pointing my behaviour out to me, and I mean that. It is actually quite appreciated. My sincere apologies. This is not the place for explanations or excuses, so I will not try and give any. I am going to have to learn to stay away when, hmmmmm, conditions render my usual (I hope) polite self into something unfortunately not that way.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top