How can I create a macro in Word for a “best before” date of one year from today that will repeat, just as today's date will in Word when invoked?
1 Answers
Do you mean just generating “12/23/2018”?
- Type
Date \@ “MM/dd/”=date \@ “yyyy”+1(or copy and paste it) into your Word document.
This seems to be case-insensitive (except in the date formats).
And "straight quotes" (
)
and “curly quotes” (
) seem to be interchangeable. - Select
Date \@ “MM/dd/”and press Ctrl+F9, which will change it into a field. It will change in appearance to{ Date \@ “MM/dd/” }and may have a gray background. This same transformation will occur every time you press Ctrl+F9. Do not type any curly braces! - Select
date \@ “yyyy”and press Ctrl+F9. - Select
={ date \@ “yyyy” }+1and press Ctrl+F9. The line should now look like:
{ Date \@ “MM/dd/” }{ ={ date \@ “yyyy” }+1 }. - Then select the entire line and press F9 (not Ctrl+F9).
- If nothing happens, press Shift+F9 (again, not Ctrl+F9), and that should do it.
This simply takes today’s date, formats it as “MM/dd/” (i.e., “12/23/”),
and then follows it with the current year plus one.
(If you want another format, just change it.)
For example, if you want "yyyy/MM/dd",
change the starting text to =date \@ “yyyy”+1Date \@ “/MM/dd”
and follow the same instructions
(with the difference that MM/dd/ has been replaced with /MM/dd,
since you want the / between the year and the month,
and not after the day).
I’m not exactly sure what you mean by “save this as a macro and name it”. Maybe this is what you want:
- Enter
Set best_date quote =date \@ “yyyy”+1Date \@ “/MM/dd”. You might want to do this somewhere near the beginning of the document. - Follow all the Ctrl+F9 steps from above.
- Select
quote { ={ date \@ “yyyy” }+1 }{ Date \@ “/MM/dd” }(i.e., everything fromquotethrough the end of the line) and press Ctrl+F9. - Select
Set best_date { quote { ={ date \@ “yyyy” }+1 }{ Date \@ “/MM/dd” } }(i.e., the entire line) and press Ctrl+F9. - Repeat the last two steps from the above list
(the ones with F9 and Shift+F9).
Now, everything you’ve just done will disappear,
but you will have defined a “bookmark” called
best_datecontaining the next year’s date. This is for all practical purposes a variable; it might be close enough to what you mean when you say “macro”. - Now, anywhere in the document that you want to use the future date,
type
best_dateand turn it into a field as described above (Ctrl+F9, F9 and Shift+F9).
best_date is just an arbitrary identifier.
You can use any valid identifier, e.g., bob_string.
Warning: if you use this on February 29, 2020, it will display “2021/2/29”, which is not a valid date.
OK, in a comment you reported that, when you save and reopen your document, the fields appear, like
{ Set best_date { quote { ={ date \@ “yyyy” }+1 }{ Date \@ “/MM/dd” } } }
and the date does not appear. This is controlled by a global Word setting. There are at least a couple of ways of fixing it:
- The way Microsoft tells you to do it:
- Go into “Word Options”. On sufficiently old versions of Word (pre-2010), this is accessed via the “Orb”. On Word 2010 and 2013 (and higher?), it is “File” → “Options”.
- Go to “Advanced” (by clicking on “Advanced” in the left column of the Options window).
- Scroll down to the “Show document content” section.
- Click on “Show field codes instead of their values” to clear (uncheck) the checkbox.
- A much shorter way:
- Press Alt+F9. Once. In any document.
You will, of course, have to do this (one of the above) once on every machine. Probably once per machine per user. Or just tell your users to do it (I recommend the Alt+F9 one) if they see field codes displayed.
I’m not sure I understand your other issue. Are you saying that, if you follow the above instructions on December 28, 2017, you will get the “2018/12/28” future date that you want, but if you save it, and then reopen it on December 29, it will still say “2018/12/28”, but you want it to say “2018/12/29”? This is a little trickier. Apparently Microsoft Word is written to not update fields when you open a document (possibly because it could theoretically take a long time). Here are two solutions to that:
-
- select the entire document (the easy way to do this is to press Ctrl+A), and
- press F9.
Unfortunately, every user will have to learn to do this every time they open a document (or at least each day).
As described here, write an
AutoOpenmacro that saysSub AutoOpen() With Options .UpdateFieldsAtPrint = True .UpdateLinksAtPrint = True End With ActiveDocument.Fields.Update End SubSee How do I add VBA in MS Office? for general information on the mechanics of this.
I recommend option 1; macros can be messy. But, if your users are resistant to learning new procedures, option 2 may be a better choice for you.