[Word VBA] How to Automatically Insert Today’s Date on the Second Line | Right-Aligned “Update Date”

When creating documents in Word, do you ever find it tedious to manually type the date, such as “Update Date,” every time?

In this article, I will introduce a method using Word VBA to automatically insert today’s date in the format “Update Date: May 1, 2024” into the second line of a document.

目次

What We Want to Achieve

  • Insert the date in the second line (second paragraph) of a Word document.
  • Format it as “Update Date: [Month] [Day], [Year]”.
  • Automatically fetch today’s date.
  • Right-align the text.

This eliminates the hassle of manual entry and ensures the document’s update date is always accurate.

The VBA Code

Executing the code below will update the second paragraph of the document and insert today’s date.

Sub ReplaceSecondParagraphWithDate()
    Dim doc As Document
    Set doc = ActiveDocument

    ' Check if the document has at least 2 paragraphs
    If doc.Paragraphs.Count >= 2 Then
        Dim secondParagraph As Paragraph
        Set secondParagraph = doc.Paragraphs(2)

        ' Get today's date in "Month Day, Year" format
        Dim formattedDate As String
        formattedDate = Format(Date, "mmmm d, yyyy")

        ' Overwrite the 2nd line with "Update Date: [Date]"
        ' vbCrLf adds a line break
        secondParagraph.Range.Text = "Update Date: " & formattedDate & vbCrLf

        ' Set alignment to Right
        secondParagraph.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    Else
        MsgBox "The document needs at least 2 paragraphs."
    End If
End Sub

Key Points

Format(Date, "mmmm d, yyyy")

This formats the current date (Date) into a style like “May 1, 2024”.

  • mmmm = Full month name (e.g., May, December)
  • d = Day
  • yyyy = 4-digit Year

Explicit Line Break with vbCrLf

By adding vbCrLf (carriage return and line feed) to the end of the text, we ensure that the inserted text does not merge with the following paragraph. This is a measure to ensure it functions correctly as a Word paragraph.

Right Alignment with wdAlignParagraphRight

Setting ParagraphFormat.Alignment to wdAlignParagraphRight aligns the entire paragraph to the right. This is effective for organizing the visual layout of documents.

Important Notes

  1. Overwriting Warning: This macro completely overwrites the content of the second line. If there is already text there, it will be lost, so please save or back up important information beforehand.
  2. Paragraph Check: To avoid errors, the code displays a message and stops processing if the document has fewer than two paragraphs.

Summary

By using Word VBA, you can automate routine entries like “Update Date.”

  • Insert “Update Date: [Date]” into the 2nd paragraph of the document, right-aligned.
  • Keep the document’s update date constantly current.
  • Highly effective for preventing manual errors and improving efficiency.

If you find yourself typing the date every time for Word-based business reports or proposals, please try this automation code.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次