Help with embedded "If"

S

Suzanne

I'm trying to merge Excel data to Word based on two values: "UIC" and "Room"

{IF {MERGESEQ } = 1 ""}
{ SET UIC1 { MERGEFIELD UIC }}
{ SET ROOM1 { MERGEFIELD ROOM }}
{ IF { UIC2 } <> { UIC1 } { AND { IF { ROOM2 } <> { ROOM1} }}
"{MERGEFIELD UIC } { MERGEFIELD ROOM } {MERGEFIELD LAST }"
"{MERGEFIELD UIC } { MERGEFIELD ROOM } {MERGEFIELD LAST }"
{ SET UIC2 {MERGEFIELD UIC } {SET ROOM2 {MERGEFIELD ROOM } }}

This is partially working:

- It's skipping records
- Where each skipped record is supposed to be, there's an "Error!
Bookmark not defined" message.
 
P

Peter Jamieson

Assuming you're doing a "one-many" type merge
There are at least two problems in your current code:
a. you can't do the AND the way you are trying to do it at the moment
b. you seem to be inserting the same thing whether UIC and ROOM change or
not.

If you only want to test for a change in either UIC or ROOM, you can try

{ IF { MERGESEQ } = "1" "{ SET LUIC "" }{ SET LROOM "" }" ""
}{ If "{ LUIC }{ LROOM }" <> "{ MERGEFIELD UIC }{ MERGEFIELD ROOM }"
"{ MERGEFIELD UIC }{ MERGEFIELD ROOM }

" "" }whatever you want here
{ SET LUIC { MERGEFIELD UIC } }{ SET LROOM { MERGEFIELD ROOM } }

or perhaps even just

{ IF { MERGESEQ } = "1" "{ SET UICROOM "" }" ""
}{ If "{ UICROOM }" <> "{ MERGEFIELD UIC }{ MERGEFIELD ROOM }"
"{ MERGEFIELD UIC }{ MERGEFIELD ROOM }

" "" }whatever you want here
{ SET UICROOM "{ MERGEFIELD UIC }{ MERGEFIELD ROOM }" }

If you are trying to check that both UIC and ROOM have changed, try

{ IF { MERGESEQ } = "1" "{ SET LUIC "" }{ SET LROOM "" }" ""
}{ If { ={ COMPARE { LUIC } <> { MERGEFIELD UIC } }+{ COMPARE { LROOM } <>
{ MERGEFIELD ROOM } } } = 2
"{ MERGEFIELD UIC }{ MERGEFIELD ROOM }

" "" }whatever you want here
{ SET LUIC { MERGEFIELD UIC } }{ SET LROOM { MERGEFIELD ROOM } }

All the above assume that UIC and ROOM are never ""
 
S

Suzanne

Thanks! The third one did it (I didn't try the other two). You are correct
in that none of the fields are empty.

I've given up hope on this, but here's one last shot... now that the data is
in Word, is there anyway to automate my Word form with these results? I was
told I couldn't do it straight from Excel.

I have a two-page form with numerous table columns and rows. The UIC,
Building, and Room go on the top of the first page. Names which are
associated with that UIC/Bldg/Room combination go on the second page.

The second page is not in a table format. Rows of names are at the top of
the page.

I'm ready to conclude that despite the great things that Word can do, I may
have to manually align, cut/paste, and validate data on the form I'm required
to use.

Suz
 
P

Peter Jamieson

Hi Suzanne

As Doug mentioned in an earlier thread, it's partly a question of "where to
put your page break".

whatever you put in the TRUE result of the IF field here...

{ If { ={ COMPARE { LUIC } <> { MERGEFIELD UIC } }+{ COMPARE { LROOM } <>
{ MERGEFIELD ROOM } } } = 2
"{ MERGEFIELD UIC }{ MERGEFIELD ROOM }

" "" }

is going to appear when UIC and ROOM change (so that probably needs to have
a page break at the end - you can insert one in the result text) and perhaps
you will also need to repeat some of the fields at the beginning of that new
page. i.e. you need something like

{ If { ={ COMPARE { LUIC } <> { MERGEFIELD UIC } }+{ COMPARE { LROOM } <>
{ MERGEFIELD ROOM } } } = 2
"{ MERGEFIELD UIC }{ MERGEFIELD ROOM }
whatever else you want on page 1 followed by
<page break> (put a real page break in here)
followed by whatever text and fields you need at the top of page 2
" "" }

At the moment, my suggestion assumes that the stuff you have in "whatever
you want here" will be output for every record. This is because in this kind
of merge, the first record in the group also usually contains the data for
the first detail record in the group IYSWIM. What I am not sure about in
this case is what you mean by...
The second page is not in a table format. Rows of names are at the top of
the page.

Can you provide an example of the kind of layout you need to achieve? If you
need to avoid having paragraph marks between each record, you need to
eliminate any paragraph marks in the mail merge main document that would be
output following any of the records, and that probably also means that you
need to insert a page break at the beginning of the "heading texts", e.g.

{ If { ={ COMPARE { LUIC } <> { MERGEFIELD UIC } }+{ COMPARE { LROOM } <>
{ MERGEFIELD ROOM } } } = 2
"<page break>{ MERGEFIELD UIC }{ MERGEFIELD ROOM }
whatever else you want on page 1 followed by
<page break> (put a real page break in here)
followed by whatever text and fields you need at the top of page 2
" "" }

If you need to eliminate the page break on page 1, try

{ If { ={ COMPARE { LUIC } <> { MERGEFIELD UIC } }+{ COMPARE { LROOM } <>
{ MERGEFIELD ROOM } } } = 2
"{ IF { LUIC } <> "" "<page break>" "" }{ MERGEFIELD UIC }{ MERGEFIELD
ROOM }
whatever else you want on page 1 followed by
<page break> (put a real page break in here)
followed by whatever text and fields you need at the top of page 2
" "" }

NB, you cannot eeliminate the final paragraph mark on a page, but a
directory merge does not insert that one in the output anyway, i.e. if your
mail merge main document just ha

{ MERGEFIELD UIC }

and nothing else except that final para, your output would have

UIC1UIC2UIC3UIC4...

You may find in practice that this introduces other problems (for one thing,
I hope I have the logic the right way around!) but try it anyway.
 
S

Suzanne

I can't get this working... I've been able to get new record breaks after
each UIC, but have yet to see just the list of names on the second page.

A simplified version of what I'm trying to get to:

First Page: UIC BLDG ROOM (followed by rows/columns of text)
Second Page: All people with the same UIC and Room (everyone in the room
will have the same building number but not necessarily the same UIC)

I can adjust the first page of the form so that one table cell will contain
UIC, BLDG, ROOM. On the top of the second page, I need a list of names for
that UIC+Room.

Without your help (hopefully), I'm still stuck at having to manually Alt-Tab
between two document to cut/paste the information on the forms.

Thank you VERY much...

Suz
 
S

Suzanne

After a full day of hopefully waiting for someone to tell me that there is
some method of doing what I need, I'll close this out and get on to
cutting/pasting the data for each record (sigh).

Thanks Peter for your help... Suz
 
P

Peter Jamieson

I was away for a couple of days - if you still need a solution I'll have
another look.
 
S

Suzanne

Peter,

You're a lifesaver. Look for my post "I almost have the 2-page merge" in
this thread.

When/if you open my Word doc (it's on my Office Live site:
http://suzleigh.com/MERGE.aspx), it will look a little mangled. Regardless,
I really think that I may almost have this nailed down.

I was able to get this far by creating a new column in my Excel worksheet:
"UICROOM" -- UIC & ROOM (this might be helpful for anyone in the future who
is having the same problem). On plain paper, I get exactly the results I'm
looking for. I still can't get the results on the actual form.

If you look at the form, you'll also see that I switched the pages to avoid
having to sift through rows/columns of data to get to the names listing (the
name listing is now page 1; the detailed form is page 2).

Would love your help (and/or anyone else who might be listening in).

Suz
 
P

Peter Jamieson

1. I think it's a bit easier to follow the logic if you stick to using one
local variable (and the pattern I suggested before, where you explicitly set
LOC to "" in that first record, then test LOC against the value of
MERGEFIELD UICROOM, and set LOC again at the end of the mail merge main
document.

2. Because of the way this technique works, it's easiest to imagine that the
list of names always comes last. Because you actually want some notes etc.
after the list of names, when you use this technique, you then have to put
those notes at the very beginning and eliminate them when MERGESEQ is 1 1.
So your merge then looks something like

{ IF { MERGESEQ } = 1 "{ SET LOC "" }{ SET NOTES "" }" "" }
{ IF "{ LOC }" = "{ MERGEFIELD UICROOM }"
"{ NOTES }
<your section 1 fields and table>
<page or {section break>
<your section 5 page header and column headers" "" }
<merge fields you want in the list of names>{ SET LOC { MERGEFIELD
UICROOM }{ SET NOTES "whatever you want in the notes at the end of this
group of records<page or section break> }

3. The trouble with this is that the notes under your last list of names
will never be emitted. But because of the way Word works, I don't think you
can logically achieve what you need without also ensuring that you know
which the last record in each group is. If you know that, e.g. because you
mark the last recors in each group, you can do something more like

{ IF { MERGESEQ } = 1 "{ SET LOC "" }" "" }
{ IF "{ LOC }" = "{ MERGEFIELD UICROOM }"
"<your section 1 fields and table>
<page or {section break>
<your section 5 page header and column headers" "" }
<merge fields you want in the list of names>{
{ IF { MERGEFIELD LASTRECORDINGROUP } = "Y" "output your note text here"
"" }{ SET LOC { MERGEFIELD UICROOM }

Sorry, it's now rather late here so I may not be thinking straight, but
Ireally think that's what you need to do.
 
S

Suzanne

I tried your last example. It's not working.

The result had the first two names at the top and did not pull everyone
together.

I've been messing with it; at one point I had the names grouped, but
somewhere along the line I messed that up. I'm getting a variety of results
as I continue to mess with this (error codes, misplaced data, skipped
records).

At this point I'm not even working with the actual form but rather a plain
page test document. I think if I can get the plain page worked out, I can
apply it to the real thing.

Thanks -- Suzanne

I still think the answer is close.
 
S

Suzanne

I tried your last example. It's not working.

The result had the first two names at the top and did not pull everyone
together.

I've been messing with it; at one point I had the names grouped, but
somewhere along the line I messed that up. I'm getting a variety of results
as I continue to mess with this (error codes, misplaced data, skipped
records).

At this point I'm not even working with the actual form but rather a plain
page test document. I think if I can get the plain page worked out, I can
apply it to the real thing.

Thanks -- Suzanne

(I still think the answer is close)
 
P

Peter Jamieson

Can you try opening the following URL in your browser, right-clicking on the
link and downloading the .doc in there?

http://tips.pjmsn.me.uk/suzanne

This .doc should demonstrate the /first/ solution I suggested (although the
layout probably needs fixing in various ways). If not, can you please
describe what it does not do, apart from the known problem of dealing with
the notes in the last record.

If you would find it easier to correspond by e-mail, please despam my e-mail
(remove KillmapS) and email me.
 
P

Peter Jamieson

BTW, you may need to select the mail merge main document using e.g.
control-A and press F9 to re-execute all the fields
 
S

Suzanne

YOU ARE AWESOME!!!!!!! I KNEW this could be done!! Thank you for not
giving up on this or telling me that what I needed to do was not possible!

The form works PERFECTLY. I've already entered the rest of my fields with
absolutely no problem and even restructured the first so that the UIC, BLDG,
etc. is within the table.

This will absolutely be a 'read-only' document forever and ever (I do NOT
want to go through this again -- although, I now know more about merge codes
than I used to!).

THANK YOU THANK YOU THANK YOU!!!

Suzanne
 
P

Peter Jamieson

Hi Suzanne

(Phew!)

If you haven't checked the very last page of output from this merge, please
do so. If it's OK, we can all relax. Otherwise, maybe that approach I
mentioned - where the last record in each group needs to be marked - needs
to be used. Or maybe it would be enough to add that chunk manually.
 
S

Suzanne

Yes... the last page didn't have the notes, but this is an easy cut/past (I
might also try putting a dummy record at the end of the worksheet). MUCH
MUCH easier to think about than having to cut/paste each section of people
into their respective rooms.

I just ran a report with nearly 200 people in different buildings, rooms,
UICs -- front and back report was 70 pages. That's 70 pages I didn't have to
spend wearing out Alt-Tab keys!

Now that the worst is over, there's a couple of things I'll be trying out,
but won't be as frustrated with if I don't get it. You may have noticed the
"No. CIV" "No. MIL" etc. fields at the bottom of section 2... I'm trying out
some methods to count the number of "DAC" "AD-A" "AD-AF" in section 2 and
plunk the number into the appropriate field in section 1.

Again, you are AWESOME!! Have a GREAT weekend!!!

Suzanne
 
P

Peter Jamieson

Hi Suzanne,

thanks for the feedback- as for the counts etc., my only suggeston is to try
to create the data source you need, including any "count" columns - it's
probably simpler than trying to add up stuff using field codes.
 

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