在word文档中插入代码_如何在自动更新的Word文档中插入当前的月份和年份

在word文档中插入代码

在word文档中插入代码

00_lead_image_insert_month_and_year

You can easily add the current date and time to your Word documents, but what if you only want to add the month or month and year, but no date? It’s easy to manually add a custom date field and we’ll show you how.

您可以轻松地将当前日期和时间添加到Word文档中,但是如果您只想添加月份或月份和年份却不添加日期怎么办? 手动添加自定义日期字段很容易,我们将向您展示如何操作。

Adding a date field to your Word document using the Field command on the Insert tab adds the complete date, without an option to only add the month or year. However, you can manually create a date field with only the items you want in different formats. We’ll show you how to insert the month and year using a field code and how to change the format of the month and year using switches.

使用“插入”选项卡上的“字段”命令将日期字段添加到Word文档中会添加完整的日期,而不能选择仅添加月份或年份。 但是,您可以手动创建日期字段,仅包含所需格式不同的项目。 我们将向您展示如何使用域代码插入月份和年份,以及如何使用开关更改月份和年份的格式。

In our example, we’re creating a title page for our monthly TPS Report and want to include the current month and year. We can create this as a template that we can use each month to quickly create the cover page, or we can just use the same document each month and update the date field.

在我们的示例中,我们为每月的TPS报告创建标题页,并希望包含当前的月份和年份。 我们可以将其创建为模板 ,以便每个月使用它来快速创建封面,或者我们每个月都可以使用相同的文档并更新日期字段。

To add a custom date field to your document, press Ctrl+F9 to insert braces to contain the field. You must use Ctrl+F9 to insert the braces and NOT just type the braces using the keyboard.

要将自定义日期字段添加到文档中,请按Ctrl + F9插入大括号以包含该字段。 您必须使用Ctrl + F9插入大括号,而不仅仅是使用键盘输入大括号。

01_inserting_braces_for_field

We’re going to add the month and year for our example, so type the following inside the braces.

我们将为示例添加月份和年份,因此在大括号内键入以下内容。

date \@ "MMMM YYYY"

The MMMM and YYYY are switches that tell Word what format to use for the month and year.

MMMM和YYYY是告诉Word在月份和年份中使用哪种格式的开关。

Click “Update”, or press F9 (with the cursor inside the field code or the field code selected), to update the field and display the month and year.

单击“更新”,或按F9(光标位于域代码或域代码内),以更新域并显示月份和年份。

02_entering_field_code_for_month_and_year

The field code above inserts the full month and year. However, you can format the month and year differently by changing the switches. Here is a list of the switches you can use for the month and year and what they produce:

上面的域代码将插入完整的月份和年份。 但是,您可以通过更改开关来格式化月份和年份。 以下是可用于月份和年份的开关及其产生的清单:

  • M: Displays the month as a number without a leading zero (0) for single-digit months. For example, August is “8”.

    M:将月份显示为一个数字,其中不包含前导零(0)。 例如,八月是“ 8”。
  • MM: Displays the month as a number with a leading zero (0) for single-digit months. For example, August is “08”.

    MM:将月份显示为一个数字,其中前几位为零(0)。 例如,八月是“ 08”。
  • MMM: Displays the month as a three-letter abbreviation. For example, August is “Aug”.

    MMM:将月份显示为三个字母的缩写。 例如,八月是“八月”。
  • MMMM: Displays the full name of the month, e.g., “August”.

    MMMM:显示月份的全名,例如“ August”。
  • YY: Displays the year as two digits with a leading zero (0) for years 01 through 09. For example, 2016 is displayed as “16”, and 2009 is displayed as “09”.

    YY:在01到09年之间以两位数字显示前导零(0)。例如,2016显示为“ 16”,而2009显示为“ 09”。
  • YYYY: Displays the year as four digits, e.g., 2016.

    YYYY:将年份显示为四位数,例如2016。
03_date_field_with_month_and_year_updated

To make changes to the field code, right-click in the field and select “Toggle Field Codes” from the popup menu.

要更改域代码,请右键单击该域,然后从弹出菜单中选择“切换域代码”。

03a_selecting_toggle_field_codes

You can also insert the month and date separately. This is useful if you want to use them in different places. For example, to insert just the current month, enter date \@MMMM in the braces (as shown below). Or, to insert just the current year, enter date \@YYYY . Note that when using only one switch (MMMM or YYYY), you don’t need quotes around the switch.

您也可以分别插入月份和日期。 如果要在不同的地方使用它们,这将很有用。 例如,要仅插入当前月份, date \@MMMM在大括号中输入date \@MMMM (如下所示)。 或者,仅输入当前年份,请输入date \@YYYY 。 请注意,仅使用一个开关(MMMM或YYYY)时,不需要在开关周围加上引号。

04_entering_date_field_code

You can also type the date field code, such as date @\ “MMMM YYYY” , and then select the text and press Ctrl+F9 to add the braces around the text and convert it to a field code.

您也可以键入日期字段代码,例如date @\ “MMMM YYYY” ,然后选择文本,然后按Ctrl + F9在文本周围添加大括号并将其转换为字段代码。

To update all the fields in a document, press Ctrl+A to select the entire document (except for textboxes, headers, footers, footnotes, and endnotes). Then, press F9 to update the fields. To update fields in textboxes, headers, footers, footnotes, and endnotes, you must go into those parts of the document separately, select the fields, and then press F9 to update them.

要更新文档中的所有字段,请按Ctrl + A选择整个文档(文本框,页眉,页脚,脚注和尾注除外)。 然后,按F9更新字段。 要更新文本框,页眉,页脚,脚注和尾注中的字段,必须分别进入文档的那些部分,选择字段,然后按F9键来更新它们。

翻译自: https://www.howtogeek.com/267499/how-to-insert-the-current-month-and-year-in-a-word-document-that-automatically-updates/

在word文档中插入代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值