Calculation fields not working with dates

两盒软妹~` 提交于 2019-12-01 13:30:30
Cindy Meister

Charles Kenyon has spent some time gather up the work of many people and made it available on his website: http://www.addbalance.com/word/datefields2.htm

The underlying trick for calculating with dates is to break them down into day, month and year, which you're attempting, but using the VBA approach that doesn't work in Word fields. Indeed, Word has no "smarts" whatsoever when it comes to date calculation - you have to do it all. So, simple example:

theDate + 1 = { DocProperty theDate \@ "MMMM" } { = {DocProperty theDate \@ "dd" } + 1 }, { DocProperty theDate \@ "yyyy" }

This is fine, until you hit the last day of the month. And then you have to consider what to do if it's the last day of the year...


This is very complex, so I'm just going to copy the information from Charles's site:


Here's a head-spinner! What follows is a field that gives you the date two weeks from the day a document is created, in regular written English format:

  • Creation date: July 9, 2015 - desired field result = July 23, 2015
  • Creation date: July 23, 2014 - desired field result = August 6, 2014
  • Creation date: December 20, 2014 - desired field result = January 3, 2015
  • Creation date: February 27, 2015 - desired field result = March 12, 2015
  • Creation date: February 27, 2016 - desired field date result = March 13, 2016

    { QUOTE "{ SET " Delay" "14" } { SET "DaysInMonth" { IF { CreateDate \@ "MM" } <> 2
    { = ROUND(30.575*{ CreateDate \@ "MM" },0)- ROUND(30.575*{ = { CreateDate \@ "MM" } -1 },0) } { IF { = MOD( { CreateDate \@"yy" } , 4 ) } > 0 "28" "29" } } } { SET "NextMonth" { IF { CreateDate \@ "MM" } = 12 "1/97" "{ = { CreateDate \@ "MM" } + 1 }/97 } } { IF { = { REF "Delay" } + { CreateDate \@ "dd" } } <= { REF"DaysInMonth" } { CreateDate \@ "MMMM { = { REF "Delay" } + { CreateDate \@ "dd" } }, yyyy"}{ QUOTE { NextMonth \@ "MMMM" } { = { REF "Delay" } + { CreateDate \@ "dd" } - { REF"DaysInMonth" } }, { IF { CreateDate \@ "MM" } <> 12 { CreateDate \@ "yyyy" } { CreateDate \@ "{ = 1 + { CreateDate \@ "yyyy" } # "xxxx" }" } } } }" }

The colors in the sample field above (below in image!) represent my attempt to match field delimiters and show nesting of fields. Line breaks have been added for clarity and should not be a part of the actual field. I added these modifications help illustrate what the field looks like. Those fields shown in green contain no nested fields but may contain references to bookmark values set by other fields.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!