![]() |
#1
|
|||
|
|||
![]()
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! |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
I have gone through that but genuinely don't understand it
|
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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 ![]() |
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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
![]() |
#8
|
|||
|
|||
![]()
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:
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") 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
So how to automatically place the calculated date? Code:
Selection.GoTo What:=wdGoToBookmark, Name:="DueDate" Selection.InsertBefore Format((Date + 21), "d MMMM yyyy") |
#9
|
||||
|
||||
![]()
It really is not that hard. See attached.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 01-31-2025 at 04:52 AM. |
#10
|
|||
|
|||
![]()
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? |
#11
|
||||
|
||||
![]()
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] |
#12
|
|||
|
|||
![]()
thank you, will it not automatically update when you open the word document?
|
#13
|
||||
|
||||
![]()
Not unless you use a macro to update fields that runs automatically when the document is opened.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
![]()
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). |
#15
|
||||
|
||||
![]()
A Print Preview won't update that field.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
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 |
![]() |
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 |