PDA

View Full Version : Solved: For..Each routine - reversing the array order



stecstec
11-25-2005, 02:50 AM
I've got a master document where I need to open the subdocuments and print them in the order they appear in the master. So far, I've managed to code this (see below) but they print in reverse order they appear in the master document.

Would there be any way of reversing the order of the array for the subdocuments using the "For..Each" routine?

cheers, steve.
Sub PrintAll()

Dim objDoc As Document
Dim sDocName As String

sDocName = ActiveDocument.Name
On Error Resume Next

For Each objDoc In Application.Documents
If InStr(objDoc, sDocName) = 0 Then
objDoc.PrintOut
End If
Next objDoc

For Each objDoc In Application.Documents
If InStr(objDoc, sDocName) = 0 Then
objDoc.Close
End If
Next objDoc


Set objDoc = Nothing

End Sub


Sub OpenSubDocs()
Dim SubDoc As Subdocument

For Each SubDoc In Selection.Range.Subdocuments
SubDoc.Open
Next SubDoc

Call PrintAll

End Sub

Killian
11-25-2005, 04:38 AM
Hi Steve and welcome to VBAX :hi:

Would it not be easier to do it in one routine? It would certainly keep things in order...Sub PrintSubDocs()

Dim SubDoc As Subdocument
Dim TempDoc As Document

For Each SubDoc In Selection.Range.Subdocuments
Set TempDoc = SubDoc.Open
TempDoc.PrintOut
TempDoc.Close
Next SubDoc

End Sub

stecstec
11-28-2005, 02:35 AM
Thanks Killian, this would help tidy the code I've done. Do you know of any way I could reverse the print order when using "TempDoc.PrintOut"?

cheers, steve.

Killian
11-28-2005, 06:54 AM
I'm not sure you would need to using the shorter version of the code...
The reason the order was reversed in your original code is that you open the subdocuments in the correct order, (say, for 3 subdocuments) 1,2,3
... but then you go to the application's documents collection, which will start with the most recent (active) document, hence 3,2,1.
If you're still getting the problem, then you could try referring the the subdocs by their index instead.
I also noticed you were using selection.range - there may be a reason why you've done this, but I suspect that opening and closing the subdocuments may cause the selection to change - it might be a good idea to set the range you're working with - or the whole source document - to an object variable at the start, then refer to thatSub PrintSubDocs()

Dim SourceDoc As Document
Dim SubDoc As Subdocument
Dim TempDoc As Document

Set SourceDoc = ActiveDocument
For Each SubDoc In SourceDoc.Subdocuments
Set TempDoc = SubDoc.Open
TempDoc.PrintOut
TempDoc.Close
Next SubDoc

End Sub
'or
Sub PrintSubDocs2()

Dim SourceDoc As Document
Dim TempDoc As Document
Dim i As Long

Set SourceDoc = ActiveDocument
For i = 1 To SourceDoc.Subdocuments.Count
Set TempDoc = SourceDoc.Subdocuments(i).Open
TempDoc.PrintOut
TempDoc.Close
Next i

End Sub

fumei
11-28-2005, 07:30 AM
Bravo.

stecstec
11-29-2005, 04:11 AM
Hey thanks very much for the solution - very much appreciated.

cheers, stec.

aja
08-30-2007, 07:44 AM
Proud of you Steve. Keep up the doodling