In this article, we will show you an interesting trick to insert a future or past date in your Word Document via VBA codes.
Generally, we have no difficulty in inserting a current date in our Word document. Yet, now and then, we could be preparing for the document needed next week or month or even later. Then here is the problem you can’t bypass. How can you insert a future date in Word file? And sometimes how about a past one even?
It is therefore we think you must grasp this useful method to better fulfill your work tasks.
Use VBA Codes to Insert a Future or Paste Date
- To begin with, open your Word document.
- Then, click “Developer” tab.
- Next click “Visual Basic” to open the VBA editor. If the “Developer” tab is not available in your Word Ribbon, you can press “Alt+ F11” to open the editor instead.
- Now in the editor, click “Normal” on the left side.
- Then click “Insert” on the toolbar.
- Next, choose “Module” from the list-menu.
- Now double click the newly inserted module, and the editing area shall be available on the right side.
- Just paste the following codes there:
Sub InsertFutureOrPastDate() Dim strNumberOfDays As String ' Input the number of days you want to insert from today to the future or past date. strNumberOfDays = InputBox("Please input the number of days you want to insert", "future or past date", "Input here.For exemple,input 1 to insert the date of tomorrow") ' Insert the future or past date according to the inputed number of days If strNumberOfDays <> "" Then Selection.TypeText Text:=Format(Date + strNumberOfDays, "dddd, MMMM dd, yyyy") End If End Sub
- Next, click “Save” button and close the VBA editor.
- Now switch back to Word document screen and place your cursor at the place where the future or past date goes.
- Then click “Developer” tab again.
- And click “Macros” in “Code” group to open the “Macros” window. Again if the “Developer” tab is not displayed, you just need to press “Alt+ F8”.
- Now in “Macros” window, find and select the “InsertFutureOrPastDate” macro.
- Next click “Run”.
- Then you shall be greeted by a message box. You can enter a number in the text box at the bottom of the box to represent the days left to the future date. For example, if you want to insert the date of tomorrow, you should input “1” there. Likewise, if you need a past date, just add a minus sign before the value, such as “-1” for yesterday.
- Remember to click “OK”.
Note: In codes “Selection.TypeText Text:=Format(Date + strNumberOfDays, “dddd, MMMM dd, yyyy”)”, the “dddd, MMMM dd, yyyy” represent the date format where “M” stands for “month”, “d” for “day”, “y” for “year” and “dddd” for “week”, so you are free to change it as you like.
Regain Your Valuable Files
Word is susceptible to collapses which throws our Word files in danger. Now and then, some of our priceless data are just gone. But what’s gone is not completely gone. You can sit to lament over the loss of your critical files or you can choose a proficient damaged Word recovery tool to retrieve them.
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including Excel xls file problem repair and pdf repair software products. For more information visit www.datanumen.com