Loops

M

Monika Krug

Is it possible to have some kind of loop or recursion with
mail merge fields?

Somewhat like this:

{ SET total { MERGEFIELD number }}
{ SET index 1 }
{ WHILE { index } < { total }
{ MERGEFIELD "Name{index}" }
{ SET index { = index + 1 } }
}

Or any kind of workaround that achieves the same?

Monika.
 
P

Paul Lautman

Monika Krug said:
Is it possible to have some kind of loop or recursion with
mail merge fields?

Somewhat like this:

{ SET total { MERGEFIELD number }}
{ SET index 1 }
{ WHILE { index } < { total }
{ MERGEFIELD "Name{index}" }
{ SET index { = index + 1 } }
}

Or any kind of workaround that achieves the same?

Monika.

In trying to assist in this I have to ask why you wish to do this. There may
be another way to accomplish what you wish.
 
C

Cindy M -WordMVP-

Hi Monika,

There's a discussion about one-to-many types of mail merge on
my website (multiple items / record). I'm not sure if this is
what you want, but there is a link to a KB article that
describes how to set up such a loop.
Is it possible to have some kind of loop or recursion with
mail merge fields?

Somewhat like this:

{ SET total { MERGEFIELD number }}
{ SET index 1 }
{ WHILE { index } < { total }
{ MERGEFIELD "Name{index}" }
{ SET index { = index + 1 } }
}

Or any kind of workaround that achieves the same?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
M

Monika Krug

Cindy said:
There's a discussion about one-to-many types of mail merge on
my website (multiple items / record). I'm not sure if this is
what you want, but there is a link to a KB article
[http://support.microsoft.com/?kbid=105888] that
describes how to set up such a loop.

I have read those, and your website as well as the KB
article were very helpful in getting to the point where I
am at now.

The problem is with: "with a copy of the conditional
statements equal to the maximum number of projects for
each employee". I have an estimate of typically 1 to 4
records, so I have inserted the statements five times ...
but maybe there could be more, so I wondered if there is a
way (without macros) to duplicate them a certain number of
times that is read from the data source. Also, the copying
is verbose and error-prone when making changes. But I
guess there is no other solution.

I have now inserted a conditional statement that prints
out a warning that all the following data will be
corrupted when there are too many records.

Monika.
 
C

Cindy M -WordMVP-

Hi Monika,

Either I'm not understanding you, or you haven't quite
grasped the everything in the KB article. The article does
show you how to set up a looping sequence. But you have to
have the data set up the right way:

EmployeeID, ProjectDetail
EmployeeED, ProjectDetail

Sorted by EmployeeID. When you do this, the mail merge will
process each record in turn. The SET field notes the current
Employee ID. Within the IF field you have both the text for
the repeating data (project), as well as the text that
preceeds and follows the repeated listing. As long as the
EmployeeID doesn't change, the list is repeated. When it
changes, the other text is inserted (including the first item
in the list).
The problem is with: "with a copy of the conditional
statements equal to the maximum number of projects for
each employee". I have an estimate of typically 1 to 4
records, so I have inserted the statements five times ...
but maybe there could be more, so I wondered if there is a
way (without macros) to duplicate them a certain number of
times that is read from the data source. Also, the copying
is verbose and error-prone when making changes. But I
guess there is no other solution.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
M

Monika Krug

Cindy said:
Hi Monika,

Either I'm not understanding you, or you haven't quite
grasped the everything in the KB article. The article does
show you how to set up a looping sequence. But you have to
have the data set up the right way:

EmployeeID, ProjectDetail
EmployeeED, ProjectDetail

Sorted by EmployeeID. When you do this, the mail merge will
process each record in turn. The SET field notes the current
Employee ID. Within the IF field you have both the text for
the repeating data (project), as well as the text that
preceeds and follows the repeated listing. As long as the
EmployeeID doesn't change, the list is repeated. When it
changes, the other text is inserted (including the first item
in the list).

I understand that, and that is also what I have done. As I
said in my last post: The problem I tried to solve was
that the maximum number of projects per employee needs to
be known beforehand and the statements need to be copied
that often.

Monika.

---
 
C

Cindy M -WordMVP-

Hi Monika,
As I
said in my last post: The problem I tried to solve was
that the maximum number of projects per employee needs to
be known beforehand and the statements need to be copied
that often.
Yes, that's where we're not communicating. If the data is
set up so that it simply lists the projects, as far as mail
merge is concerned, it doesn't matter how many there are. It
simply loops for as many projects as an employee has; you
don't make the copies, mail merge does.

So, assuming we are both understanding that point correctly,
the only way I can interpret your problem is that you need
to limit the number of projects in the data source, itself?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 
M

Monika Krug

Cindy said:
If the data is
set up so that it simply lists the projects, as far as mail
merge is concerned, it doesn't matter how many there are. It
simply loops for as many projects as an employee has; you
don't make the copies, mail merge does.

You are wrong on that. Read http://support.microsoft.com/?
kbid=105888 very closely and/or try it yourself. It does
not loop for as many projects as an employee has. The
person, not mail merge, has to make as many copies of the
conditional statement as is the maximum number of projects
per employee.

Regards,
Monika.
 
D

Doug Robbins

The only other way is to use a macro.

Create a Catalog (on in Word XP and later, it's called Directory) type
mailmerge main document with the mergefields in the cells of a one row table
in the mailmerge main document with the keyfield in the first cell in the
row and then execute that merge to a new document and then run the following
macro, it will create separate tables with the records for each key field in
them. With a bit of further development, you may be able to get it to do
what you want.

' Macro to create multiple items per condition in separate tables from a
directory type mailmerge

Dim source As Document, target As Document, scat As Range, tcat As Range
Dim data As Range, stab As Table, ttab As Table
Dim i As Long, j As Long, k As Long, n As Long
Set source = ActiveDocument
Set target = Documents.Add
Set stab = source.Tables(1)
k = stab.Columns.Count
Set ttab = target.Tables.Add(Range:=Selection.Range, numrows:=1,
numcolumns:=k - 1)
Set scat = stab.Cell(1, 1).Range
scat.End = scat.End - 1
ttab.Cell(1, 1).Range = scat
j = ttab.Rows.Count
For i = 1 To stab.Rows.Count
Set tcat = ttab.Cell(j, 1).Range
tcat.End = tcat.End - 1
Set scat = stab.Cell(i, 1).Range
scat.End = scat.End - 1
If scat <> tcat Then
ttab.Rows.Add
j = ttab.Rows.Count
ttab.Cell(j, 1).Range = scat
ttab.Cell(j, 1).Range.Paragraphs(1).PageBreakBefore = True
ttab.Rows.Add
ttab.Cell(j + 1, 1).Range.Paragraphs(1).PageBreakBefore = False
For n = 2 To k
Set data = stab.Cell(i, n).Range
data.End = data.End - 1
ttab.Cell(ttab.Rows.Count, n - 1).Range = data
Next n
Else
ttab.Rows.Add
For n = 2 To k
Set data = stab.Cell(i, n).Range
data.End = data.End - 1
ttab.Cell(ttab.Rows.Count, n - 1).Range = data
Next n
End If
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
C

Cindy M -WordMVP-

Hi Monika,

Been a long time since I read the article... I see what
you're talking about, BUT you should be able to set it up the
way I described. I've done it before, and so have others
(which is why it's been so long since I've read the article.

1. You don't need an extra column for "checked". You can test
the equivalent of "Employee".

2. You don't need NextIf, either.

Take a look at the following set of fields plus text. The ¶
is a paragraph mark (where you press ENTER). We start by
checking whether this is the first record, and if it is,
display the introductory text. Then we set the primary test
for the value on which the records are sorted. We do the
comparison, insert the text, then set the second test. As
each merge record cycles, the new value is checked against
the old value. Notice, too, which the static text is present
in both "true" and "false" conditions.

{ If { MERGEREC } = 1 "Employee: { MERGEFIELD "EmployeeID" }¶
" }{ Set test { MERGEFIELD "EmployeeID" } }¶
{ IF { Ref test } = { Ref testchange } "Item: { MERGEFIELD
"ProductName" }" "¶Closing text ---Page Break---
Employee: { MERGEFIELD "EmployeeID" }¶

Item: { MERGEFIELD "ProductName" }" }{ Set testchange {
MERGEFIELD "EmployeeID" } }
You are wrong on that. Read http://support.microsoft.com/?
kbid=105888 very closely and/or try it yourself. It does
not loop for as many projects as an employee has.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun
8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top