简体   繁体   English

在 MS Word 插入字段中获取明天的日期

[英]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.我正在尝试在 MS Word 中插入日期字段,该字段将显示我们打开文档的明天日期。

I can insert Today's date {DATE \\@ "dd/MM/yyyy"}, can we insert tomorrow's date using modified formula?我可以插入今天的日期 {DATE \\@ "dd/MM/yyyy"},我们可以使用修改后的公式插入明天的日期吗?

Thanks谢谢

Without VBA, the calculation is possible, but not straightforward, because the Word field language has very limited support for date-related operations.如果没有 VBA,计算是可能的,但不是直接的,因为 Word 字段语言对日期相关操作的支持非常有限。

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.最初我认为 Word 会在您打开或关闭/重新打开文档时自动更新 DATE,但进一步的实验表明,即使是这里的第二个建议也不会这样做。

In the specific case described (add 1 day), you should be able to use the following field coding:在描述的特定情况下(添加 1 天),您应该能够使用以下字段编码:

{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.所有{}必须是特殊字段代码大括号对,您可以使用 ctrl-F9 在 Windows Word 上插入这些特殊字段代码,并且(通常)在 Mac Word 上使用 cmd-F9 或 fn-cmd-F9,具体取决于您的键盘设置。 You can change the format at the end (" \\@DD/MM/YYYY " ) as required.您可以根据需要更改末尾的格式 (" \\@DD/MM/YYYY " )。

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.但是,当您打开文档时,这组域代码可能不会由 Word 自动更新,因此用户需要选择域代码并按 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.我最初认为 Word 会使用以下编码更新打开和/或关闭/重新打开的日期,但现在我相信我错了。 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:它在最新版本的 Windows Word 上实现的一件事是在“气泡”中显示日期字段,并提供更新字段的选项:

{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.注意,这也依赖于这样的假设:当您以“YYYY-MM-DD”格式指定日期时,Word 始终正确识别月份和日期,而不管区域设置如何,换句话说,“2016-04-01”是总是被认为是 2016 年 4 月 1 日,永远不会是 2016 年 1 月 4 日。如果有人可以提供反例,那么假设是错误的,字段编码需要更改,并且可能需要依赖于语言环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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