This section shows you how to create mailing labels from an Outlook Contact list using
the MailMerge object in Word. Word recognizes a wide variety of mailing labels.
These label types are identified by string names. You can get a list of the label types
available by clicking Envelopes and Labels on the Tools menu, clicking the Labels
tab, clicking Options, and then examining the Product number list box.
The CreateContactsLabels procedure, shown below, creates the mailing
labels. It first inserts text and placeholder strings into a document. It calls the
subroutine, FormatRange, to prepare a Range object with the
fields needed for the mailing labels. FormatRange is sent a MailMergeFields collection
object. It finds and replaces each placeholder string,phField, with the appropriate mail
merge field. Using placeholders guarantees that the MailMerge fields are inserted
exactly where you want.
The CreateContactLabels procedure then stores the range as an
AutoText entry. It uses the CreateNewDocument method of the MailingLabel
object to create the mailing labels based on the AutoText entry.
The CreateContactLabels procedure also shows how the DataSource
object can be used to filter or sort records. This object's QueryString property is
filled with an SQL string before the MailMerge object is executed.
When you call the CreateContactLabels procedure, you need to supply a string that
names the specific label type. For example, to create Avery 5160 labels, use the following
code:
CreateContactsLabels "5160"
The sample code for this section is in the template WrdSamp.dot. To run the sample
code, copy WrdSamp.dot to your Office Templates directory, create a new document based on
the WrdSamp.dot template, and click Quick Labels on the Tools menu. Choose a
label from the drop-down list box, and then click Create Labels.
Note Because CreateContactsLabels works with the MailMerge
object, it can create mailing labels from the Outlook Contacts folder without a reference
to the Outlook object library.