Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Jul 2005
    Location
    Tampa, Florida, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    COMPARE function in IF...THEN...ELSE (2003)

    Can anyone point me to a good site that gives a novice like me the step-by-step instructions on inserting =AND and =OR statements (along with COMPARE) in an IF...THEN...ELSE statement MS Mail merge? Inserting IF...THEN...ELSE is easy and inserting the function delimiters {} using CTRL-F9 is easy but in every example I see it shows something like this: { IF { = AND ( { COMPARE { MERGEFIELD CustomerNumber } >= 4}, { COMPARE { MERGEFIELD CustomerRating } <= 3})} = 1 "Satisfactory" "Unsatisfactory"}

    How do I get the: "COMPARE {" into the statement?

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Welcome to Woody's Lounge.

    Each field { something } is inserted in one of the following ways:
    - Press Ctrl+F9 to insert the brackets { } - you should NEVER type the brackets yourself, they won't work.
    - Select Insert | Field...
    - Use the options from the mail merge toolbar.

    As the example shows, you can nest one field within another field; this is done by displaying the field codes, placing the insertion point within a field and using one of the methods above. In complicated situations, I often create the "inner" field separately, in an empty part of the document, then cut and paste it into another field.

    So you don't create "COMPARE {", you create a field { COMPARE }, then insert a merge field into it, between the word COMPARE and the closing bracket, either by clicking there and using one of the methods mentioned above, or by creating the merge field elsewhere in the document, then cutting and pasting it into the COMPARE field.

  3. #3
    New Lounger
    Join Date
    Jul 2005
    Location
    Tampa, Florida, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Hans,

    Thanks for the reply. I have tried what you said and I cannot figure out how to get just 1 '{" to appear. If I try to cut and paste a '{' from another part of the statement
    it copies the entire 'piece' (everything from the left { to the right }). Does the statement have to be created from the inside out?
    Without attempting to try your patience can you tell me the keystrokes to create the statement I gave in the example. Like I said, I can do an Insert|field but it puts the right } in a position where I do not want one. Here is my feeble attempt (aside: (I wished I had followed the thread that showed how to cut & Paste the formula's right out of Word now :-( {IF {=AND ({COMPARE}}{MERGEFIELD ID} >= 4 "Satisfactory" "Unsatisfactory"}

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    The field brackets { } belong together. You cannot create them separately, and you cannot manipulate them separately. What you should do is:
    - Select { MERGEFIELD ID } >= 4
    - Cut it to the clipboard (Ctrl+X)
    - Position the insertion point immediately after COMPARE, before the closing bracket }
    - Type one space.
    - Paste the contents of the clipboard (Ctrl+V)
    - You will now have { IF { =AND ( { COMPARE { MERGEFIELD ID } >= 4 } } "Satisfactory" "Unsatisfactory" }
    - Select { COMPARE { MERGEFIELD ID } >= 4 } taking care NOT to include the second closing brakcet } immediately after it.
    - Copy the selection to the clipboard (Ctrl+C)
    - Position the insertion point between the two closing brackets } }
    - Type a comma, then paste, and type a closing parenthesis )
    - You will now have { IF { =AND ( { COMPARE { MERGEFIELD ID } >= 4} , { COMPARE { MERGEFIELD ID } >= 4 } ) } "Satisfactory" "Unsatisfactory" }

    You'll note that I inserted some spaces for readability; in most fields you can do this without affecting the way they work.

  5. #5
    New Lounger
    Join Date
    Jul 2005
    Location
    Tampa, Florida, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Hans,

    Good tips and I think I'm a lot closer (while documenting this I'm at almost 10 steps, sheesh could MS have made this any harder?)

    My example (which is almost identical to what yours was) is attached. When I compile it it still fails. I'm going to give you the steps I am going through and hopefully you can show me the 'error of my ways'. This is building it from scratch. (I preparing a class on how easy it is to do IF/THEN/ELSE in MS Word Mail Merge (hah!))

    1) Using MS Word Merge Toolbar 'Insert Word Field' and pick IF...THEN...ELSE from the list. The 5 parms are: ID, >=, 2, OK, BAD
    2) From the menu bar Insert,Field click the 'Formula' button, pick AND from the 'Paste function' drop down and click OK.
    3) Position cursor between the parens of the AND and From the menu bar Insert,Field and pick COMPARE
    4) (and this may be where I'm going wrong) Delete "* MERGEFORMAT" from within the COMPARE statement
    5) Cut { MERGEFIELD ID } >= 2 and paste if between the parens after =AND
    6) Cut "OK" "BAD" and paste them before the last }
    7) Place a comma after } and before the ) (see attachment 2 labelled: midpoint)
    8) Copy { COMPARE {MERGEFIELD ID} >= 2} and paste it after the comma and before the closing paren )
    9) Change >=2 to <= 3
    10) Compile the IF...THEN...ELSE statement. It fails with "A field calculation error failed in record 1, 2, 3,...

    Thanks again for all your help. I have tried 'slowing down' and compiling along the way but once I get into the =AND and the COMPARE (their linked) it fails.

    Again, thanks for the assistance.

    John
    Attached Files Attached Files

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    The reason it's hard is that this kind of operation is alien to Word, Excel is much better at formulas.

    If you look at the examples in the online help, you'll see that you have to put =1 after the {=AND(...) } field, 1 corresponding to True. See screenshot below.
    Attached Images Attached Images
    • File Type: png x.png (3.7 KB, 3 views)

  7. #7
    New Lounger
    Join Date
    Jul 2005
    Location
    Tampa, Florida, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Hans,

    Again thanks for all your help. I still can't get this to work. I am getting the error: Error! Unknown op code for conditional.
    (I did put in the macro I found elsewhere on this site. Great site!) When I inserted the =AND function it did not 'put in' the =1 that you suggested
    so I typed it in. Is that allowed?

    Here is my 'field construction' as it stands now:

    { IF { =AND ({ COMPARE { MERGEFIELD ID } >= 200000},{ COMPARE { MERGEFIELD ID } <= 300000} ) } = 1 "OK" "BAD" }

    Do you see anything obvious or can you point me to some 'step-by-step' site (or even a book or MS web site) that explains how to do this.
    Obviously I cannot figure it out.

  8. #8
    New Lounger
    Join Date
    Jul 2005
    Location
    Tampa, Florida, USA
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Hans,

    Again I want to thank you for your time and patience. I got it!

    { IF { =AND({ COMPARE * MERGEFORMAT { MERGEFIELD ID } >= 200000},{ COMPARE * MERGEFORMAT { MERGEFIELD ID } <= 300000}) } = 1 "OK" "BAD" }

    Thanks again & have a great day!

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    I don't know of a step-by-step tutorial.

    Could you attach a stripped down copy of the document? You can remove all text except the field you're trying to construct.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 29-Jul-05 12:47. )</P>For diagnostic purposes, it sometimes is useful to put all the different parts of the equation in your document so you can see whether the comparisons that are nested inside your field "equation" are producing the expected results. For example, is the >= comparison working correctly as a numeric or a string comparison or neither?

    Added: This is the problem with tabbed browsers. By the time you get to the second tab, two other people have posted and made your comments irrelevant! Anyway, as a bonus, take a look at <post#=320,421>post 320,421</post: >, which makes it easier to document complex field codes for the Lounge.

  11. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: COMPARE function in IF...THEN...ELSE (2003)

    Hi RECerious,

    If you take a look at my Word Field Maths 'tutorial' in <post#=365442>post 365442</post#>, you'll see that you can also achieve the same result without the COMPARE operator or, for that matter, the AND operator. You also don't need the * MERGEFORMAT switch.

    Cheers


    Chers
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •