PDA

View Full Version : Solved: Macro not cycling through all open files



clhare
03-21-2008, 08:21 AM
I have a macro that needs to run on all open templates, but for some reason it only runs on a few files, then stops. I just updated 16 templates using this macro and had to run the macro 5 times to get through them all. Is there a better way to cycle through that makes sure there are no more open files before quitting? Here's what I currently have in this macro:

Dim objDoc As Word.Document

' Update one document at a time
For Each objDoc In Documents
objDoc.Activate

' ... perform my updates

' Save and close file
ActiveDocument.Close savechanges:=wdSaveChanges

' Update next active document
Next objDoc

Any help would be greatly appreciated!

mdmackillop
03-21-2008, 08:32 AM
Hi Cheryl,
I don't know ith this will do any better, but give it a whirl.


' Update one document at a time
For i = 1 To Documents.Count
Documents(1).Activate

' ... perform my updates

' Save and close file
Documents(1).Close savechanges:=wdSaveChanges

' Update next active document
Next i

clhare
03-21-2008, 10:02 AM
It works!!

:hifive:
Thank you so much!

fumei
03-22-2008, 07:11 AM
Why Activate?

Dim objDoc As Word.Document
For Each objDoc In Documents
objDoc.whatever ' your changes
objDoc.Close savechanges:=wdSaveChanges
Next

You do not need to Activate each document.