简体   繁体   中英

Get tomorrow's date in MS Word insert field

I'm trying to insert date field in MS Word that will display tomorrow's date we one opens the document.

I can insert Today's date {DATE \\@ "dd/MM/yyyy"}, can we insert tomorrow's date using modified formula?

Thanks

Without VBA, the calculation is possible, but not straightforward, because the Word field language has very limited support for date-related operations.

Originally I thought Word would auto-update the DATE when you open or close/re-open the document, but further experiments suggest that even the second suggestion in here will not do that.

In the specific case described (add 1 day), you should be able to use the following field coding:

{QUOTE {SET xxx { DATE }}{SET yyy {xxx \@YYYY}}{SET mmm {xxx \@M}}{SET xxx1 {={xxx \@YYYYMMDD}+1 \#0000'-'00'-'00}}{=13-{xxx1 \@M} \#"'{xxx1}';'{=mmm-11 \#'{=yyy+1}-01-01';'{yyy}-{=mmm+1 \#00'-01'}'"}'"} \@DD/MM/YYYY} 

All the {} must be the special field code brace pairs that you can insert on Windows Word using ctrl-F9, and (typically) on Mac Word using cmd-F9 or fn-cmd-F9, depending on your keyboard setup. You can change the format at the end (" \\@DD/MM/YYYY " ) as required.

However, that set of field codes probably will not be updated automatically by Word when you open the document, so the user would need to select the field codes and press F9.

I originally thought Word would update the date on open and/or close/re-open using the following coding, but I now believe I was wrong. The one thing it does achieve on recent versions of Windows Word is to present the Date field in a "bubble" with an option to update the field:

{DATE \@"'{QUOTE {SET xxx { DATE }}{SET yyy {xxx \@YYYY}}{SET mmm {xxx \@M}}{SET xxx1 {={xxx \@YYYYMMDD}+1 \#0000'-'00'-'00}}{=13-{xxx1 \@M} \#"'{xxx1}';'{=mmm-11 \#'{=yyy+1}-01-01';'{yyy}-{=mmm+1 \#00'-01'}'"}'"} \@DD/MM/YYYY}'"}

Here is some pseudocode for the algorithm:

Set xxx to the date.
Set yyy to the 4-digit year
Set mmm to the month
Set xxx1 to the date but with the day number incremented by 1. e.g., for 2016-12-31, that would be a string, "2106-12-32"
This is the tricky bit:
Try to extract the month from that date using { xxx1 \@M }. If the date is valid, { xxx1 \@M } will return a valid month number, i.e. in the range 1 to 12. If the date is not valid, { xxx1 \@M } will return xxx1, e.g. "2106-12-32", which the { = } field will treat as a calculation, i.e. "year-(a maximum of 12+32=44)", so it is always going to return a number larger than 12.
If xxx1 is a valid date then
  result=xxx1
Else 'xxx1 is not a valid date so...
  If mmm (the original month) is 12 then
    result = "(yyy+1)-01-01"
  Else
    result = "yyy-(mmm+1)-01"
  End If
End If
Apply the date format you want to "result".

NB, this also relies on the assumption that Word always correctly recognises the month and day when you specify a date in the format "YYYY-MM-DD", regardless of the locale, in other words that "2016-04-01" is always recognised as 01 April 2016, never as 04 January 2016. If anyone can provide a counter-example, then the assumption is wrong, the field coding will need to change, and will probably need to be locale-dependent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM