PDA

View Full Version : Window order



mdmackillop
04-04-2007, 11:24 AM
I'm running some code which creates from 1 to 4 letters in new windows, The following snippet creates the letters and brings one to the front. My original document, with the userform etc. is sitting in second place. I'd like to move it to the back of the visible windows, so that the new letters are all available in turn.


MultiLetters
Windows(1).Activate

mvidas
04-04-2007, 11:48 AM
What about something like Dim WND As Window
Set WND = ActiveDocument.Windows(1)
MultiLetters
WND.ActivateMatt

mvidas
04-04-2007, 11:51 AM
Oops I think I misunderstood your question.. what about Dim WND As Window, WN As Window
Set WND = ActiveDocument.ActiveWindow
MultiLetters
For Each WN In Application.Windows
If WN <> WND Then WN.Activate
Next

mdmackillop
04-04-2007, 12:19 PM
Hi Matt. That bring it to the front. I want it at the back.
I Tried MultiLetters
For i = Windows.Count To 2 Step -1
Windows(i).Activate
Next
but no consistency.

mdmackillop
04-04-2007, 12:23 PM
Oops I think I misunderstood your question.. what about Dim WND As Window, WN As Window
Set WND = ActiveDocument.ActiveWindow
MultiLetters
For Each WN In Application.Windows
If WN <> WND Then WN.Activate
Next
It ends up as second in order. Very strange.

mdmackillop
04-04-2007, 12:25 PM
BTW, all new window files start with an incrementing number, the original has a text name, so nothing alphabetical going on.

mvidas
04-04-2007, 12:34 PM
Every time I think I have it, I run it a second time and it doesn't make logical sense. very strange indeed.

Hmm.. try this, seems to work every time:Sub MDExample()
Dim WND As Window, WN As Window, WNS As Collection, i As Long
Set WNS = New Collection
Set WND = ActiveDocument.ActiveWindow
MultiLetters
For Each WN In Application.Windows
If WN <> WND Then WNS.Add WN
Next
For i = WNS.Count To 1 Step -1
WNS(i).Activate
Next
End Sub
Function MultiLetters()
Dim i As Long
For i = 1 To 4
Documents.Add
Next
End Function

mdmackillop
04-04-2007, 12:35 PM
I've maybe found part of the reason. I'm using Application.Visible = False to avoid all the flickering (screen updating doesn't suffice).

mdmackillop
04-04-2007, 12:38 PM
If only multiletters were so simple!

Sub MultiLetters()
Dim JobNo$, MyPath$, DataFile$, MyRec$, MDoc$, Recpt$, NewName$
Dim i As Long
JobNo = Mid(ActiveDocument.Path, 4, 4)
MyPath = ActiveDocument.Path & "\"
DataFile = MyPath & "Data.doc"
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) = True Then
MyRec = Split(ListBox2.List(i), "-")(0)
'Add Datasource to selected document
Documents.Open FileName:=MyPath & "MergeLetters\" & ListBox1
ActiveDocument.MailMerge.OpenDataSource Name:=DataFile, _
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1 _
:=""
'Add Sender
User
'Set filter to merge only selected addressee
ActiveDocument.MailMerge.DataSource.QueryString = _
"SELECT * FROM " & DataFile & " WHERE ((Rec = " & MyRec & "))" & ""
MDoc = ActiveDocument.Name
Recpt = ActiveDocument.MailMerge.DataSource.DataFields(3).Value
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
'Save file with appropriate name
DoSave MyPath, Recpt
NewName = ActiveDocument.FullName
'Close merge document
Documents(MDoc).Close wdDoNotSaveChanges
'Delete single use documents
DoDelete MyPath, NewName
End If
Next
End Sub

mdmackillop
04-04-2007, 12:41 PM
Don't worry about it Matt. I'll take the easy option and close the b@8$$?y thing!

mvidas
04-04-2007, 12:55 PM
It will always work if you set the .windowstate to minimized.. just wont be there after they go through the 4 windows.
And oops.. didn't mean to include my MultiLetters function, I just did that to test with