0
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?
0
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?
0
Do you mean just generating “12/23/2018”?
Date \@ “MM/dd/”=date \@ “yyyy”+1
(or copy and paste it)
into your Word document.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!date \@ “yyyy”
and press Ctrl+F9.={ date \@ “yyyy” }+1
and press Ctrl+F9.
The line should now look like:{ Date \@ “MM/dd/” }{ ={ date \@ “yyyy” }+1 }
.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:
Set best_date quote =date \@ “yyyy”+1Date \@ “/MM/dd”
.
You might want to do this somewhere near the beginning of the document.quote { ={ date \@ “yyyy” }+1 }{ Date \@ “/MM/dd” }
(i.e., everything from quote
through the end of the line)
and press Ctrl+F9.Set best_date { quote { ={ date \@ “yyyy” }+1 }{ Date \@ “/MM/dd” } }
(i.e., the entire line) and press Ctrl+F9.best_date
containing 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”.best_date
and 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:
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:
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 AutoOpen
macro that says
Sub AutoOpen()
With Options
.UpdateFieldsAtPrint = True
.UpdateLinksAtPrint = True
End With
ActiveDocument.Fields.Update
End Sub
See 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.
Thanks so much, G-Man. I got this to work in the format provided. I would prefer it to read year/month/day, but when I try to get it to do this it prints out incorrectly as 2017 +1 then the month and day. Is there some way I can save this as a macro and name it? – Bob – 2017-12-27T18:03:20.383
I have expanded my answer in an attempt to address your concerns. – G-Man Says 'Reinstate Monica' – 2017-12-29T05:11:34.110
Hello again, G-Man. My need is to have a formula which will appear on repeated uses as an actual date, so that others can use it without having to convert it. When I save the updatable future date, it either comes up next day as a formula again, or as yesterday's date in the following year with no repeatable action--the formula is lost. I need to be able to place this info into dozens of BarTender label files, so that each time one is opened, it will provide the future date one year hence: 2018/12/30, and so forth. I'm still floundering. – Bob – 2017-12-30T14:40:31.117
@Bob: Happy New Year. I have expanded my answer in an attempt to address your concerns (again). – G-Man Says 'Reinstate Monica' – 2018-01-05T20:37:55.517
@Bob: Are you still “floundering”? If my answer works for you, please “accept” it.
– G-Man Says 'Reinstate Monica' – 2018-01-27T15:17:25.093Hello G-Man: Yes, it did for Word Documents. I was trying to utilize this in a labeling program called BarTender, but it would not translate from Word into that medium. However, I found that BarTender itself offers a way to insert a best before date, so of course I am using that. Much thanks for your help. – Bob – 2018-01-28T20:09:17.700