Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-06-2024, 06:30 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default Add 21 days onto Auto Update field

Hi all



I have a word template that I want an Auto Update field that automatically adds 21 days onto todays date how can I do this? I tried the Formula =DATE()+21 and =TODAY()+21 but both come back as a Syntax error

Many Thanks!
Reply With Quote
  #2  
Old 11-06-2024, 01:40 PM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The process for that is explained in the Microsoft Word Date Calculation Tutorial 'Sticky' thread at the top of this forum: https://www.msofficeforums.com/word/...-tutorial.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-08-2024, 04:51 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default

I have gone through that but genuinely don't understand it
Reply With Quote
  #4  
Old 11-11-2024, 02:13 PM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You need to do little more than copy the Calculate a day, date, month and year, using n days delay example from the tutorial. Having pasted the field into your own document, select it and press Shift-F9 to display the field code. Then all you need do is change the 14 in SET Delay 14 in the field code to 21 and press F9 to update the field. Done. It's that simple.

Code:
{QUOTE
{SET Delay 14}
{SET a{=INT((14-{DATE \@ M})/12)}}
{SET b{={DATE \@ yyyy}+4800-a}}
{SET c{={DATE \@ M}+12*a-3}}
{SET d{DATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045+Delay}}
{SET e{=INT((4*(jd+32044)+3)/146097)}}
{SET f{=jd+32044-INT(146097*e/4)}}
{SET g{=INT((4*f+3)/1461)}}
{SET h{=f-INT(1461*g/4)}}
{SET i{=INT((5*h+2)/153)}}
{SET dd{=h-INT((153*i+2)/5)+1}}
{SET mm{=i+3-12*INT(i/10)}}
{SET yy{=100*e+g-4800+INT(i/10)}}
"{dd}-{mm}-{yy}" \@ "dddd, d MMMM yyyy"}
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-13-2024, 07:06 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default

Hello,
So I have inserted an auto update Date Field, I have then gone into the Field code and typed in your code below bu comes up with errors
Reply With Quote
  #6  
Old 11-13-2024, 01:43 PM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by stu_c View Post
So I have inserted an auto update Date Field, I have then gone into the Field code and typed in your code below bu comes up with errors
You really do need to learn to pay attention to what is written. I did not say to type in the field code representation from my previous reply. I said to edit the field code that you were to copy from the tutorial. I even explained how to do the editing.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-13-2024, 05:06 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default

Unfortunately I have spent considerable amount of time looking at this and I genuinely have no idea what to do, I wouldn't keep asking if I knew
Reply With Quote
  #8  
Old 12-13-2024, 11:27 AM
Charles Kenyon Charles Kenyon is offline Add 21 days onto Auto Update field Windows 11 Add 21 days onto Auto Update field Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,533
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Stu,

I appreciate your frustration. This may be easy for Paul. He mastered it long ago and provided a wonderful reference. If you use it as intended, you can get what you want.

Calendar math is far from simple. You cannot simply type calculated date fields because dates do not follow ordinary math rules.

The following is from my page on calculated date fields:
Again, Paul's tutorial is the reference source on these.

Adding 21 days to a date is not simple.

Creation date: July 9, 2015 - desired field result = July 30, 2015
This one is easy because it is in the same month and year. It could be done with a simple field.

Creation date: June 23, 2014 - desired field result = July 14, 2014
Creation date: July 23, 2014 - desired field result = August 13, 2014
Oops: Still in the same year, but a different month. How many days were there in the original month?

Creation date: December 20, 2014 - desired field result = January 10, 2015
Oops again, not only a different month but a different year.

Creation date: February 27, 2015 - desired field result = March 19, 2015
Creation date: February 27, 2016 - desired field date result = March 20, 2016
Again, a different month. And, how many days are there in February?


What to do:
  • Download Paul's tutorial. Read the introductory material.
  • Read about the different kinds of date fields.
  • The tutorial goes into Julian and Gregorian calendar dates. You do not need to understand this to use the fields in the tutorial. You may need it to understand the construction of some of the fields, but not to use them.
  • Do use the macro in the tutorial to adjust your date format if different.
  • Copy one of Paul's base fields into a document. I suggest the one for Calculate a day, date, month and year, using n days delay.
  • Toggle the display of field codes using Alt+F9 or Fn+Alt+F9.
  • Change the Delay in that field from 14 to 21
  • Toggle the display of field codes back to showing results.
  • Press F9 (or Fn+F9) to update the field.
Again, I suggest changing the DATE field in that to a CREATEDATE field and having this in a template.

You can use a macro for this if fields are too complex.
Calculated dates are easiest performed by a short macro that has the following code -
Code:
Selection.InsertBefore Format((Date + 21), "d MMMM yyyy")
will enter the current date + 14 at the cursor.

Here is a more complex one from MVP Graham Mayor:
Code:
Sub InsertFutureDate()    'Graham Mayor
    ' Written by Graham Mayor and posted on the word.docmanagement
    '   newsgroup in March 2000
    ' Inserts a future date in a document - note that this is not a field
    ' Some style revisions and error handler by Charles Kenyon
    '
    Dim message As String
    Dim Mask   As String  ' Format for date in form "MMMM d, YYYY" will produce December 13, 2024 format
    Dim title  As String
    Dim Default As String ' Number of days to advance date
    Dim Date1  As String
    Dim myValue As Variant
    Dim MyText As String
    Dim Var1   As String
    Dim Var2   As String
    Dim Var3   As String
    Dim Var4   As String
    Dim Var5   As String
    Dim Var6   As String
    Dim Var7   As String
    Dim Var8   As String
    '
    Mask = "d MMMM yyyy"    ' Set Date format
    Default = "21"   ' Set default number of days.
    title = "Plus or minus date starting with " & Format(Date, Mask)
    Date1 = Format(Date, Mask)
    Var1 = "Enter number of days by which to vary above date. " _
           & "The number entered will be added to "
    Var2 = Format(Date + Default, Mask)     ' Today plus default (60)
    Var3 = Format(Date - Default, Mask)     ' Today minus default (60)
    Var4 = ".     The default ("
    Var5 = ") will produce the date "
    Var6 = ".  Minus (-"
    Var7 = ".  Entering '0' (zero) will insert "
    Var8 = " (today).  Click cancel to quit."
    MyText = Var1 & Date1 & Var4 & Default & Var5 & Var2 & Var6 _
             & Default & Var5 & Var3 & Var7 & Date1 & Var8
    '
    '   Display InputBox and get number of days
GetInput:
    myValue = InputBox(MyText, title, Default) ' uses variable set above
    '
    If myValue = "" Then
        End    'quit subroutine
    End If
    '
    On Error GoTo oops    ' just in case user typed non-number
    Selection.InsertBefore Format((Date + myValue), Mask)
    Selection.Collapse (wdCollapseEnd)
    End    'End subroutine
    '
oops:        ' error handler in case user types something other than a number
    '
    MsgBox Prompt:="Sorry, only a number will work, please try again.", _
           buttons:=vbExclamation, _
           title:="A number is needed here."
    GoTo GetInput
End Sub

You can get this very fancy if you wish, having it insert at bookmarked locations. From my page, again...
So how to automatically place the calculated date?
The method I tend to prefer is to use a bookmark in conjunction with an AutoNew macro that runs automatically on creating the new document from the invoice template.
Bookmark the place the due date will appear. Call the bookmark 'DueDate'.
You need an extra line of code for the macro to locate the bookmark, and you may need to change the date mask to suit local requirements. As printed below it will type '12 September 2000'
Code:
Selection.GoTo What:=wdGoToBookmark, Name:="DueDate" 
Selection.InsertBefore Format((Date + 21), "d MMMM yyyy")
Reply With Quote
  #9  
Old 12-13-2024, 01:39 PM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

It really is not that hard. See attached.
Attached Files
File Type: docx Date+21.docx (12.6 KB, 19 views)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 01-31-2025 at 04:52 AM.
Reply With Quote
  #10  
Old 01-31-2025, 04:06 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default

Hi Paul
Thank you for the code, I will start to learn better now I have a full example, only thing is it doesn't update itself?
Reply With Quote
  #11  
Old 01-31-2025, 04:52 AM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The field will update any time you do something that causes Word to refresh the fields in a document. For example, pressing Ctrl-A, then F9.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 01-31-2025, 04:58 AM
stu_c stu_c is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Novice
Add 21 days onto Auto Update field
 
Join Date: Dec 2013
Posts: 20
stu_c is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
The field will update any time you do something that causes Word to refresh the fields in a document. For example, pressing Ctrl-A, then F9.
thank you, will it not automatically update when you open the word document?
Reply With Quote
  #13  
Old 01-31-2025, 05:03 AM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Not unless you use a macro to update fields that runs automatically when the document is opened.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 01-31-2025, 02:18 PM
Charles Kenyon Charles Kenyon is offline Add 21 days onto Auto Update field Windows 11 Add 21 days onto Auto Update field Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,533
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

See also: Some Fields are Updated While Others Are Not and Dealing with Fields in Microsoft Word.
You should be able to force an update by viewing in Print Preview (not print view).
Reply With Quote
  #15  
Old 01-31-2025, 03:20 PM
macropod's Avatar
macropod macropod is offline Add 21 days onto Auto Update field Windows 10 Add 21 days onto Auto Update field Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

A Print Preview won't update that field.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Update Date Field + 21 days stu_c Outlook 0 10-30-2024 03:59 AM
Ref field to Database field doesn't update properly in Mailmerge huynguyen865 Mail Merge 1 08-30-2021 07:30 AM
Auto-update field codes in Outlook Template - AutoOpen Macro not working victoriasun Outlook 5 12-22-2015 01:54 AM
Add 21 days onto Auto Update field Auto update Filename field Oliver Beirne Word VBA 4 10-19-2012 03:33 AM
Table of Contents - Direct Field Implementation - Auto-Update Numbering ztag Word 3 01-19-2012 03:04 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft