Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Merge If Statement - Compare Date Range 1

Status
Not open for further replies.

FoundOps

Technical User
Feb 27, 2012
3
US
Hello,
I'm trying to find out if something is even possible in MS Word, but I have looked everywhere on the Internet, and nothing. I'm trying to see if a date range (from start to finish) is more than a year (365 days), and if it is, do Scenario One, otherwise do nothing.
As in:
{if {MERGEFIELD End_Date \@"YYYYMMDD"} - {MERGEFIELD Start_Date \"@YYYYMMDD"} > 365 "More than one year" ""}

I keep getting this message when I run the mail merge: "Error! Unknown op code for conditional"

What am I doing wrong? Can Word Merge not do arithmetic calculations and compare the result?

Thank you for your assistance.
 
hi,

In what application is your source data stored?

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]
 
The correct syntax is:
{if{= {MERGEFIELD End_Date \@"YYYYMMDD"} - {MERGEFIELD Start_Date \"@YYYYMMDD"}} > 365 "More than one year" ""}
but that won't do what you want. For that, you'd need something like:
{QUOTE{SET EndDate {MERGEFIELD End_Date}}{SET StartDate {MERGEFIELD Start_Date}}
{={SET a{=INT((14-{EndDate \@ M})/12)}}
{SET b{={EndDate \@ yyyy}+4800-a}}
{SET c{={EndDate \@ M}+12*a-3}}
{SET d{EndDate \@ d}}
{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-365}
-{SET a{=INT((14-{StartDate \@ M})/12)}}
{SET b{={StartDate \@ yyyy}+4800-a}}
{SET c{={StartDate \@ M}+12*a-3}}
{SET d{StartDate \@ d}}
{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)} \# "'More than one year';'Less than one year';'One year'"}}

Note: The field brace pairs (ie '{ }') for the above fields are all 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.

You can also omit the 'Less than one year' and/or 'One year' strings (but not the ; characters) if you don't need them. Also, the line breaks aren't really needed - they won't do any harm, but I left them their for clarity. You can download a macro to turn the above into a working field, from:
Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top