在word文档中插入日期

发布于 2024-09-13 11:42:39 字数 137 浏览 1 评论 0原文

我正在处理一个Word文档,每页都有一个表格。 每个表都包含多次出现“Datum”一词。双击“Datum”一词,该词应替换为系统日期。 并且这个日期应该被“冻结”,即在不同的日期打开文档时它不能自行调整。

有谁可以帮我解决这个问题的代码吗?

I'm working on a Word document which has a table on each page.
Each table contains several occurrences of the word "Datum". By double-clicking this word "Datum", this word should be replaced with the system date.
And this date should be "frozen", i.e. it mustn't adapt itself when opening the document on a different day.

Is there anyone who can help me out with the code for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-09-20 11:42:39

您可以使用宏按钮字段来执行此操作:

  • 按 Ctrl + F9 创建新字段

  • 在大括号内输入以下文本:

     宏按钮插入日期时间基准
    

    结果如下所示:

     { MACROBUTTON 插入日期时间基准 }
    
  • 按 Alt + F9 切换域代码显示

这将显示 Word 的内置插入日期对话框。如果您不想显示该对话框,可以将 InsertDateTime 替换为自定义 VBA 宏的名称,例如 MyModule.MyInsertDate。该宏将用当前日期替换该字段:

Public Sub InsertCurrentDate()
    Selection.Text = Now
End Sub

You can do that using a macro button field:

  • Press Ctrl + F9 to create a new field

  • Enter the following text within the curly braces:

      MACROBUTTON  InsertDateTime Datum
    

    The result will look as follows:

      { MACROBUTTON  InsertDateTime Datum }
    
  • Press Alt + F9 to toggle field code display

This will show Word's built-in Insert Date dialog. If you don't want to display the dialog you can replace InsertDateTime with the name of a custom VBA macro, e.g. MyModule.MyInsertDate. This macro would replace the field with the current date:

Public Sub InsertCurrentDate()
    Selection.Text = Now
End Sub
深海不蓝 2024-09-20 11:42:39

尝试将每个数据设为字段 调用 Word VBA 的 {MacroButton datumToDate Datum}:

Sub datumToDate()
    Selection.InsertDateTime Format(Now(), "yyyy-mm-dd")
End Sub

显然可以随意将 yyyy-mm-dd 编辑为任何有效的 时间格式。这应该使用 Selection 对象的 InsertDateTime 来替换“当前选择” ',此处您的字段标记为 Datum。它仅替换当前的基准,因为它偏离了您开始 Sub 时所在的位置。

要激活宏按钮字段,您必须双击,除非您

Sub AutoOpen()
    Options.ButtonFieldClicks = 1
End Sub

在 ThisDocument 位置运行。我建议不要这样做,因为有人很容易不小心单击旧表中的数据并将日期重置为“现在”,并且我不确定如果您不双击字段,选择方法将起作用(尽管它应该) 。

Try making each Datum a Field {MacroButton datumToDate Datum} that calls a Word VBA:

Sub datumToDate()
    Selection.InsertDateTime Format(Now(), "yyyy-mm-dd")
End Sub

Obviously feel free to edit yyyy-mm-dd to any valid time format. This should use Selection Object's InsertDateTime to replace the 'current selection', here your field labeled Datum. It only replaces the current Datum because it goes off of where you were when you began the Sub.

To activate MacroButton Fields, you will have to double click unless you run

Sub AutoOpen()
    Options.ButtonFieldClicks = 1
End Sub

in the ThisDocument location. I advise against this because it's easy for someone to accidentally click a Datum in an old table and reset the date to Now and I'm not positive that the Selection Method will work if you don't double click the Field (although it should).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文