Search code examples
ms-wordmailmergemergefield

MS Word MAILMERGE autonumbering for MERGEFIELD


I just learn how to use MAILMERGE in MS Word. I want to list my MERGEFIELD use alphabetic sequence and my code is like this :

{ SEQ list \* alphabetic }. { MERGEFIELD item1 }
{ SEQ list \* alphabetic }. { MERGEFIELD item2 }
{ SEQ list \* alphabetic }. { MERGEFIELD item3 }
{ SEQ list \* alphabetic }. { MERGEFIELD item4 }
{ SEQ list \* alphabetic }. { MERGEFIELD item5 }

And the result from code above is :

a.  duck
b.  dog
c.  bird
d.  cat
e.  pig

I have problem when one or more MERGEFIELD value is empty. For example if { MERGEFIELD item3 } is empty and it will produce list like this :

a.  duck
b.  dog
c.  
d.  cat
e.  pig

What I want is like this :

a.  duck
b.  dog
c.  car
d.  pig

Solution

  • If it is Windows Word 2003 or later (or possibly Word XP, I forget), the simplest approach is probably to use the following nested field codes and rely on Word to "suppress blank lines" (which is its default setting)

    { MERGEFIELD item1 \b "{ SEQ { IF { MERGEFIELD item1 } = "" nolist list } \*alphabetic }. " }
    { MERGEFIELD item2 \b "{ SEQ { IF { MERGEFIELD item2 } = "" nolist list } \*alphabetic }. " }
    { MERGEFIELD item3 \b "{ SEQ { IF { MERGEFIELD item3 } = "" nolist list } \*alphabetic }. " }
    { MERGEFIELD item4 \b "{ SEQ { IF { MERGEFIELD item4 } = "" nolist list } \*alphabetic }. " }
    { MERGEFIELD item5 \b "{ SEQ { IF { MERGEFIELD item5 } = "" nolist list } \*alphabetic }. " }
    

    (All the { } have to be the special field code brace pairs that you can insert in Windows Word using ctrl-F9. Also, when you preview, the empty lines will still appear to be present, with a ".." at the beginning, but they should disappear when you actually perform the merge.)

    If you are using Windows Word XP/2000 or earlier, or any version of Mac Word, the \b flag is not supported. So you have to use IF fields to generate the results for the blank/non-blank cases. The problem is that SE fields inside IF fields are generally evaluated whether or not the resulting text is output, so you end up with gaps in the sequence. Even trying to get Word to use two different sequences ("list" and "nolist" as above) does not work as you might hope. However, there is another approach you can try (it should work with all versions of Word):

    { IF { MERGEFIELD item1 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item1 }
    " }{ IF { MERGEFIELD item2 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item2 }
    " }{ IF { MERGEFIELD item3 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item3 }
    " }{ IF { MERGEFIELD item4 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item4 }
    " }{ IF { MERGEFIELD item5 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item5 }
    " }
    

    Notice that in this case we cannot rely on Word to suppress the blank lines resulting from an empty IF field result, so instead we put a paragraph mark at the end of the text that the IF field inserts when the MERGEFIELD result is not blank.

    Either approach should extend up to as many items as you need, but be aware that the "letter sequence" produced by *alphabetic is not

    a b . . z aa ab . . az ba bb . . bz

    as you might expect, but

    a b . . z aa bb . . zz aaa bbb . . zzz

    .

    If you actually want the first, more familiar, sequence, you will need to do more. (I have done this before but may re-research it).

    However, if you want to make it easier to extend the item count, if the fields continue to be numbered as item6, item7 etc., you can modify the field coding so that all you need to do is paste in the number of copies you need. For the coding that uses SEQ, you could use

    { MERGEFIELD "item{ SEQ i }" \b "{ SEQ { IF MERGEFIELD "item{ SEQ i \c }" } = "" nolist list } \*alphabetic }. " }
    

    For the coding that uses SET, you could use

    { IF { MERGEFIELD "item{ SEQ i}" } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD "item{ SEQ i \c }" }
    " }