Advanced Mail Merge
Budget: $30 – $250 USD
I need to create a word mail merge from an excel document to print labels with the quantity of each label printed based on the value of a field within the excel document. We also need the mail merge to make the most efficient use of the label sheets which will always contain the same number of labels. I found the following but need help to implement it.
A set of fields coded as follows can be used to vary the output quantities:
{IF{MERGEREC}= 1 {SET Idx 0}}{IF{Idx}<> {MERGEREC} "{SET Data "{MERGEFIELD FIRST_NAME} {MERGEFIELD LAST_NAME}"}{SET QTY {MERGEFIELD Quantity}}{SET Idx {MERGEREC}}"}{SET QTY {=QTY-1}}{REF DATA}{NEXTIF {QTY}= 0}
or:
{IF{MERGEREC}= 1 {SET Idx 0}}{IF{Idx}<> {MERGEREC} "{SET Data "«FIRST_NAME» «LAST_NAME»"}{SET QTY {MERGEFIELD Quantity}}{SET Idx {MERGEREC}}"}{SET QTY {=QTY-1}}{REF DATA}{NEXTIF {QTY}= 0}
The bold portion is where you insert your mergefields, together with whatever paragraphs and other formatting you require. The above example assumes data fields named 'FIRST_NAME' and 'LAST_NAME' and that the # of labels for each record is determined by a 'Quantity' data field.
The field codes as depicted above need to be copied to each label’s cell. You could use Word's 'update labels' function, but that will insert an unwanted «NextRecord» field at the start of all labels after the first, which you will need to delete from each label.
Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar. The spaces represented in the field construction are all required. For a macro to convert the above field text strings to working field codes, see 'Convert Text Representations of Fields to Working Fields' at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html.
Having constructed the label page, you need to run the following Word macro, which automates the label production:
Sub RunMultiLabelMerge()
Dim r As Single, c As Single, h As Single, v As Single
Dim i As Long, j As Long, x As Long, y As Long, z As Long
' Turn Off Screen Updating
Application.ScreenUpdating = False
With ActiveDocument
'Temporarily delete the fields (for processing speed)
.Fields.Unlink
With .MailMerge.DataSource
'Get the label count
For i = 1 To .RecordCount
.ActiveRecord = i
j = j + .DataFields("Quantity").Value
Next
.ActiveRecord = 1
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
'Restore the deleted the fields
.Undo
'Calculate the # labels per page
With .Tables(1)
With .Cell(1, 1)
r = .Height: c = .Width
End With
If .Cell(1, 2).Width = .Cell(1, 1).Width Then
h = c
x = .Columns.Count
Else
h = c + .Cell(1, 2).Width
x = -Int(-.Columns.Count / 2)
End If
If .Cell(2, 1).Height = .Cell(1, 1).Height Then
v = r
y = .Rows.Count
Else
v = v + .Cell(2, 1).Height
y = -Int(-.Rows.Count / 2)
End If
'Calculate the # label pages required
z = Int(j / (x * y))
y = .Rows.Count
'Add the required # label pages
If z > 0 Then .Range.Copy
For i = 1 To z
ActiveDocument.Paragraphs.Last.Range.Paste
Next
End With
'Execute the mailmerge
.MailMerge.Execute
'Restore the document to its original state
If z > 0 Then .Range.Paste: .UndoClear: .Saved = True
End With
' Restore original Status Bar status
Application.DisplayStatusBar = SBar
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
A set of fields coded as follows can be used to vary the output quantities:
{IF{MERGEREC}= 1 {SET Idx 0}}{IF{Idx}<> {MERGEREC} "{SET Data "{MERGEFIELD FIRST_NAME} {MERGEFIELD LAST_NAME}"}{SET QTY {MERGEFIELD Quantity}}{SET Idx {MERGEREC}}"}{SET QTY {=QTY-1}}{REF DATA}{NEXTIF {QTY}= 0}
or:
{IF{MERGEREC}= 1 {SET Idx 0}}{IF{Idx}<> {MERGEREC} "{SET Data "«FIRST_NAME» «LAST_NAME»"}{SET QTY {MERGEFIELD Quantity}}{SET Idx {MERGEREC}}"}{SET QTY {=QTY-1}}{REF DATA}{NEXTIF {QTY}= 0}
The bold portion is where you insert your mergefields, together with whatever paragraphs and other formatting you require. The above example assumes data fields named 'FIRST_NAME' and 'LAST_NAME' and that the # of labels for each record is determined by a 'Quantity' data field.
The field codes as depicted above need to be copied to each label’s cell. You could use Word's 'update labels' function, but that will insert an unwanted «NextRecord» field at the start of all labels after the first, which you will need to delete from each label.
Note: The field brace pairs (i.e. '{ }') for the above example are created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac); you can't simply type them or copy & paste them from this message. Nor is it practicable to add them via any of the standard Word dialogues. Likewise, you can't type or copy & paste the chevrons (i.e. '« »') - they're part of the actual mergefields, which you can insert from the mailmerge toolbar. The spaces represented in the field construction are all required. For a macro to convert the above field text strings to working field codes, see 'Convert Text Representations of Fields to Working Fields' at: http://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html.
Having constructed the label page, you need to run the following Word macro, which automates the label production:
Sub RunMultiLabelMerge()
Dim r As Single, c As Single, h As Single, v As Single
Dim i As Long, j As Long, x As Long, y As Long, z As Long
' Turn Off Screen Updating
Application.ScreenUpdating = False
With ActiveDocument
'Temporarily delete the fields (for processing speed)
.Fields.Unlink
With .MailMerge.DataSource
'Get the label count
For i = 1 To .RecordCount
.ActiveRecord = i
j = j + .DataFields("Quantity").Value
Next
.ActiveRecord = 1
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
'Restore the deleted the fields
.Undo
'Calculate the # labels per page
With .Tables(1)
With .Cell(1, 1)
r = .Height: c = .Width
End With
If .Cell(1, 2).Width = .Cell(1, 1).Width Then
h = c
x = .Columns.Count
Else
h = c + .Cell(1, 2).Width
x = -Int(-.Columns.Count / 2)
End If
If .Cell(2, 1).Height = .Cell(1, 1).Height Then
v = r
y = .Rows.Count
Else
v = v + .Cell(2, 1).Height
y = -Int(-.Rows.Count / 2)
End If
'Calculate the # label pages required
z = Int(j / (x * y))
y = .Rows.Count
'Add the required # label pages
If z > 0 Then .Range.Copy
For i = 1 To z
ActiveDocument.Paragraphs.Last.Range.Paste
Next
End With
'Execute the mailmerge
.MailMerge.Execute
'Restore the document to its original state
If z > 0 Then .Range.Paste: .UndoClear: .Saved = True
End With
' Restore original Status Bar status
Application.DisplayStatusBar = SBar
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub