“Best Before” Date, 1 year into the future

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?

Bob

Posted 2017-12-23T03:25:32.557

Reputation: 1

Answers

0

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" (straight quotes) and “curly quotes” (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” }+1 and 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 from quote through 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_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”.
  • Now, anywhere in the document that you want to use the future date, type 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:

  1. 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.
  2. 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:

  1. As described here (and here),

    • 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).

  2. 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.

G-Man Says 'Reinstate Monica'

Posted 2017-12-23T03:25:32.557

Reputation: 6 509

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.093

Hello 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