Let's see how comfortable you are with the kludgy version while my
coder writes a better version for you. :)
Create a mail merge main document of labels with the mail merge field
codes in it. Save it keeping it open. (If you've already got a file
like this, please save it to a new name for testing purposes.)
Hit Alt+F11 to open the Visual Basic Editor (VBE).
At left, double-click your file's name and hit Insert-Module from the
menu. Paste the following code into the window that appears at right:
START CODE-----------------
Option Explicit
Sub GetTeachers()
Dim Teachers(1 To 3) As String
Dim x As Long
Teachers(1) = "'Mrs. Jones'"
Teachers(2) = "'Mrs. Smith'"
Teachers(3) = "'Mrs. Ziggywatts'"
For x = 1 To UBound(Teachers)
ActiveDocument.MailMerge.DataSource.QueryString = _
"SELECT * FROM C:\Documents and Settings\Anne
Troy\Desktop\data.xls WHERE ((Teacher = " & Teachers(x) & "))" _
& ""
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=True
End With
ActiveDocument.PrintOut
ActiveWindow.Close 'False (Use False to not save changes)
Next x
End Sub
END CODE ----------------------------
Now, edit the code.
Change the 3 in this line to as many teachers as you have:
Dim Teachers(1 To 3)
For each teacher you have, add an additional line like this one,
incrementing the number by one each time and, of course, changing the
teacher's name each time, as you can see in the sample above:
Teachers(3) = "'Mrs. Ziggywatts'"
When you're done, hit the save diskette on the toolbar to save the
code, then close the VBE.
To test, go to Tools-Macro-Macros and double-click GetTeachers.
This macro will not only mail merge, but print. All you need to do is
close the files when asked if you want to save (which you don't).
I hope to get a better version for you, but can't promise anything.
The better version will get the names from the Excel file for you,
instead of us having to hard-code the names in the VBA code. It will
also not make you close the files. We can also make it so all you have
to do is open that file and it will automatically go print all the
teachers labels and even close itself. If you'd like all that
capability, just say so. We'll get it to you.
Oh...and I'm not a researcher, so it's all free. :)
Nice try, crythias! I didn't know there were any "office geeks" over
here, and it's great to see it. I'm always learning... |