MartySmartyPants
MartySmartyPants

Reputation: 1

Get the following Monday's date using MS Word field codes

I have a Microsoft Word 2021 document that I'd like to use a field code to insert the following Monday's date, whatever today's date is.

For example, if I open the doument on Thursday 12th December 2024, the inserted date code would be Monday 16th December 2024.

One caveat, if the document is opened on a Monday, for example, Monday 9th December 2024, the inseted date code would be that Monday (Monday 9th December 2024).

Hope that makes sense.

Upvotes: 0

Views: 61

Answers (2)

Charles Kenyon
Charles Kenyon

Reputation: 1028

Here is a field that comes close

It will be in the format Monday 9 December 2024.

{ IF { CreateDate \@ "ddd" } = "Mon" "{ CreateDate \@ "dddd d MMMM  yyyy" }" }
"{QUOTE
{SET Delay 1}
{SET Weekday 0}
{SET a{=INT((14-{CREATEDATE \@ M})/12)}}
{SET b{={CREATEDATE \@ yyyy}+4800-a}}
{SET c{={CREATEDATE \@ M}+12*a-3}}
{SET d{CREATEDATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045+Delay}}
{SET jd{=INT(jd/7)*7+Weekday}}
{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 dd{=((dd<8)+(dd>14)*(dd<22))*7+dd}}
{SET mm{=i+3-12*INT(i/10)}}
{SET Limit{=IF((mm=2),28+((MOD(yy,4)=0)+(MOD(yy,400)=0)-(MOD(yy,100)=0)),IF((mm=4)+(mm=6)+(mm=9)+(mm=11)=1,30,31))}}
{SET yy{=100*e+g-4800+INT(i/10)}}
{IF{mm}> 28 "{SET dd{=dd+14-Limit}}{SET mm{=MOD(mm,12)+1}}{SET yy{=yy+(mm=1)}}"}
"{mm}-{dd}-{yy}" \@ "dddd, d MMMM yyyy"}" }
{ IF { CreateDate \@ "ddd" } = "Mon" "{ CreateDate \@ "dddd d MMMM  yyyy" }" }
"{QUOTE
{SET Delay 1}
{SET Weekday 0}
{SET a{=INT((14-{CREATEDATE \@ M})/12)}}
{SET b{={CREATEDATE \@ yyyy}+4800-a}}
{SET c{={CREATEDATE \@ M}+12*a-3}}
{SET d{CREATEDATE \@ d}}
{SET jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045+Delay}}
{SET jd{=INT(jd/7)*7+Weekday}}
{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 dd{=((dd<8)+(dd>14)*(dd<22))*7+dd}}
{SET mm{=i+3-12*INT(i/10)}}
{SET Limit{=IF((mm=2),28+((MOD(yy,4)=0)+(MOD(yy,400)=0)-(MOD(yy,100)=0)),IF((mm=4)+(mm=6)+(mm=9)+(mm=11)=1,30,31))}}
{SET yy{=100*e+g-4800+INT(i/10)}}
{IF{mm}> 28 "{SET dd{=dd+14-Limit}}{SET mm{=MOD(mm,12)+1}}{SET yy{=yy+(mm=1)}}"}
"{mm}-{dd}-{yy}" \@ "dddd, d MMMM yyyy"}" }

It is based on one from Paul Edstein (macropod)'s tutorial. The date shown will be in the format Monday 9 December 2024 without the ordinal. I do not have the ability to format it that way. It is likely a bit more complex than would be needed.

I strongly recommend that you download that tutorial and save it.

Here is a CreateDate field formatted to use the ordinal form for the day of the month: { CreateDate \@ "dddd" } { CreateDate \@ "d" \* ordinal } of { CreateDate \@ "MMMM yyyy" }.

You should not try to type this field! Here is a link to a document containing the field and the process I used to construct it. You could copy and paste from it if it will meet your needs.

Otherwise, you could use this Add-In from MVP Graham Mayor to create the field from the text.

Here is my article on the Microsoft Answers site on Dealing with Fields.

This uses a CreateDate field rather than a Date field. Here is my page on the various date fields in Word and how they are formatted. https://addbalance.com/word/datefields1.htm#PageStart Unless you are in an unusual situation, you should be using a custom template with a CreateDate field rather than a document with a Date field. Here is a link to my page on using Templates in Microsoft Word. https://www.addbalance.com/usersguide/templates.htm

See also the vba solution suggested at How to have Word VBA TypeText 45 days after today's date, but have it display the following Monday?

Upvotes: 0

jonsson
jonsson

Reputation: 1301

If you are likely to be doing a lot of other things with dates using fields then it will almost certainly be worth looking at the solutions by macropod (e.g. here )

But if it's just this one, you could use the following field codes. Here, I have inserted some line ends for clarity. At the end of this Answer, I have put the RTF version of a Word document containing these field codes so that you don't have to enter them manually. But the main thing is to notice that all the {} pairs are the special field code characters that you can enter using ctrl-F9 in the Windows desktop version of Word. You can't just type them.

{QUOTE
  {SET a {DATE}}
  {SET D {a \@DDD \*UPPER}}
  {SET i {IF d = "MON" 0 {IF d = "SUN" 1 {IF d = "SAT" 2 {IF d = "FRI" 3   {IF d = "THU" 4 {IF d = "WED" 5 6 }}}}}}}
  {SET b {={a \@YYYYMMDD}+i \#0000'-'00'-'00}}
  {IF {b \@D} = {b} 
   "{SET m {a \@M}}
    {SET c "{={a \@YYYY}+(m=12) \#0000}-{=m*(m<12)+1 \#00}-01"}
    {SET d {c \@DDD \*UPPER}}
    {SET i {IF d = "MON" 0 {IF d = "SUN" 1 {IF d = "SAT" 2 {IF d = "FRI" 3 {IF d = "THU" 4 {IF d = "WED" 5 6 }}}}}}}
    {={c \@YYYYMMDD}+i \#0000'-'00'-'00}"

   "{b}"
  } \@DD/MM/YYYY}

But actually you should put all the fields on one line, like this:

{QUOTE {SET a {DATE}}{SET D {a \@DDD \*UPPER}}{SET i {IF d = "MON" 0 {IF d = "SUN" 1 {IF d = "SAT" 2 {IF d = "FRI" 3   {IF d = "THU" 4 {IF d = "WED" 5 6 }}}}}}}{SET b {={a \@YYYYMMDD}+i \#0000'-'00'-'00}}{IF {b \@D} = {b} "{SET m {a \@M}}{SET c "{={a \@YYYY}+(m=12) \#0000}-{=m*(m<12)+1 \#00}-01"}{SET d {c \@DDD \*UPPER}}{SET i {IF d = "MON" 0 {IF d = "SUN" 1 {IF d = "SAT" 2 {IF d = "FRI" 3 {IF d = "THU" 4 {IF d = "WED" 5 6 }}}}}}}{={c \@YYYYMMDD}+i \#0000'-'00'00}" "{b}"} \@DD/MM/YYYY}

If you need to use another date as the starting point, let's say 2024-12-07, change

  {SET a {DATE}}

to

  {SET a "2024-12-07"}

The very last line specifies the date formatting of the output, e.g. if you need M/D/Y formatting you can change

  } \@DD/MM/YYYY}

to

  } \@MM/DD/YYYY}

or whatever.

There is a suggestion in one of the places where Microsoft documented Data Field switches that it is possible to use a switch to return the day number within a week (e.g. 1 for Sunday, 2 for Monday etc.). But there isn't AFAIK. So we have to rely on comparing day names or day name abbreviations. Because these can differ, e.g. the French abbreviations are different from the English ones, you should select the two nested fields that look like this, and set the proofing language to English (UK will probably do but others will probably work too). The alternative requires even more code as you would need to do something like

SET i {IF d = {QUOTE "2024-12-09" \@DDD} 0 {IF d = {QUOTE "2024-12-08" \@DDD} 1 

Some notes on how it works:

{SET d {a \@DDD \*UPPER}}

gets the abbreviated week day name in uppercase (e.g. MON, TUE etc.)

{SET i ...etc.... }

sets the value of i to the number of days we need to add to get to the correct Monday.

{SET b {={a \@YYYYMMDD}+i \#0000'-'00'-'00}}

adds that number to the date in YYYYMMDD format, then formats it as YYYY-MM-DD so that Word recognises it as a date. Because DD is always going to be less than 32, adding up to 6 won't change MM.

But of course we may end up with an invalid date like 2024-10-35 so the following code detects that:

{IF {b \@D} = {b}

If b is not a real date, then the \@D switch will not do anything and the value of {b \@D} will match the value of {b}. But if b is a real date, {b \@D} will return the day number, and that won't match {b}

If b is a valid date, we're done. Otherwise we have to change month and get the correct date within the month. Changing month is simple enough - if it's December we add one to the year and set the month to 1, otherwise we leave the year as it is and add one to the month. There are various ways to do that but here I have set m to the old month number:

{SET m {a \@M}}

then for the year, I add the result of the boolean expression (m=12) to the old year number. In Word field coding, a boolean True evaluates to 1 and a False evaluates to 0. So

{SET c "{={a \@YYYY}+(m=12) \#0000}

To get the month we can use

{=m*(m<12)+1 \#00}

If m = 12, that is 12*0 + 1, i.e. 1, and if m < 12, it is m*1 + 1, i.e. m + 1

To get the day, we know that it must be on or after the first day of the next month, so all we really have to do is work out which DAY the first or that month is, and work with that day instead of today's date. Which is why you will see the following fields duplicated in the code:

    {SET d {c \@DDD \*UPPER}}
    {SET i {IF d = "MON" 0 {IF d = "SUN" 1 {IF d = "SAT" 2 {IF d = "FRI" 3 {IF d = "THU" 4 {IF d = "WED" 5 6 }}}}}}}

The following are the above field codes (more or less) saved in Word RTF format. To use it, copy the RTF text paste it into a new document in a plain text editor such as Notepad on Windows or TextEdit on MACOS, then save the file with the extension .rtf. You should then be able to open that file in Word and copy the field codes where you need them in your document.

{\rtf1\ansi\ansicpg1252\uc1 \deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f164\froman\fcharset238\fprq2 Times New Roman CE;}{\f165\froman\fcharset204\fprq2 Times New Roman Cyr;}
{\f167\froman\fcharset161\fprq2 Times New Roman Greek;}{\f168\froman\fcharset162\fprq2 Times New Roman Tur;}{\f169\froman\fcharset177\fprq2 Times New Roman (Hebrew);}{\f170\froman\fcharset178\fprq2 Times New Roman (Arabic);}
{\f171\froman\fcharset186\fprq2 Times New Roman Baltic;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;
\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}{\stylesheet{
\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs24\lang2057\langfe1033\cgrid\langnp2057\langfenp1033 \snext0 Normal;}{\*\cs10 \additive Default Paragraph Font;}}\paperw11906\paperh16838 
\widowctrl\ftnbj\aenddoc\noxlattoyen\expshrtn\noultrlspc\dntblnsbdb\nospaceforul\formshade\horzdoc\dgmargin\dghspace180\dgvspace180\dghorigin1800\dgvorigin1440\dghshow1\dgvshow1
\jexpand\viewkind1\viewscale217\viewzk2\pgbrdrhead\pgbrdrfoot\splytwnine\ftnlytwnine\htmautsp\nolnhtadjtbl\useltbaln\alntblind\lytcalctblwd\lyttblrtgr\lnbrkrule \fet0\sectd \linex0\headery708\footery708\colsx708\endnhere\sectlinegrid360\sectdefaultcl 
{\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta )}}{\*\pnseclvl5
\pndec\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang
{\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}\pard\plain \ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs24\lang2057\langfe1033\cgrid\langnp2057\langfenp1033 {\field{\*\fldinst 
{\lang1036\langfe1033\langnp1036 QUOTE }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 SET a }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 DATE}}{\fldrslt }}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 {\*\bkmkstart a}2024-12-31
{\*\bkmkend a}}}}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 SET d }{\field{\*\fldinst ref {a \\@DDD \\*UPPER}}{\fldrslt {TUE}}}}{\fldrslt {\lang1024\langfe1024\noproof TUE}}}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 SET i }
{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "MON" 0 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "SUN" 1 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "SAT" 2 }{\field{\*\fldinst {
\lang1036\langfe1033\langnp1036 IF d = "FRI" 3 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "THU" 4}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036  IF d = "WED" 5 6}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 6}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 6}}}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 SET b }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 =}{\field{\*\fldinst ref {
\lang1036\langfe1033\langnp1036 a \\@YYYYMMDD}}{\fldrslt {\lang1036\langfe1033\langnp1036 20241231}}}{\lang1036\langfe1033\langnp1036 +i \\#0000'-'00'-'00}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2024-12-37}}}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 {\*\bkmkstart b}2024-12-37{\*\bkmkend b}}}}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF }{\field{\*\fldinst ref {\lang1036\langfe1033\langnp1036 b \\@D}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 
2024-12-37}}}{\lang1036\langfe1033\langnp1036  = }{\field{\*\fldinst ref {\lang1036\langfe1033\langnp1036 b}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2024-12-37}}}{\lang1036\langfe1033\langnp1036  "}{\field{\*\fldinst {
\lang1036\langfe1033\langnp1036 SET m }{\field{\*\fldinst ref {\lang1036\langfe1033\langnp1036 a \\@M}}{\fldrslt {\lang1036\langfe1033\langnp1036 12}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 {\*\bkmkstart m}12{\*\bkmkend m}}}}
{\field\fldedit{\*\fldinst {\lang1036\langfe1033\langnp1036 SET c "}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 =}{\field{\*\fldinst ref {\lang1036\langfe1033\langnp1036 a \\@YYYY}}{\fldrslt {\lang1036\langfe1033\langnp1036 2024}}}{
\lang1036\langfe1033\langnp1036 +(m=12) \\#0000 }}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2025}}}{\lang1036\langfe1033\langnp1036 -}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 =m*(m<12)+1 \\#00}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 01}}}{\lang1036\langfe1033\langnp1036 -01"}}{\fldrslt {\*\bkmkstart c}{\field{\*\fldinst {\lang1024\langfe1024\noproof\langnp1036  =}{\field{\*\fldinst ref {\lang1024\langfe1024\noproof\langnp1036  a \\@YYYY }
}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2024}}}{\lang1024\langfe1024\noproof\langnp1036 +(m=12) \\#0000 }}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2025}}}{\lang1024\langfe1024\noproof\langnp1036 -}{\field{\*\fldinst {
\lang1024\langfe1024\noproof\langnp1036 =m*(m<12)+1 \\#00 }}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 01}}}{\lang1024\langfe1024\noproof\langnp1036 -01{\*\bkmkend c}}}}{\field{\*\fldinst {SET d }{\field{\*\fldinst ref {c \\@DDD \\*UPPER}
}{\fldrslt {WED}}}}{\fldrslt {\lang1024\langfe1024\noproof {\*\bkmkstart d}WED{\*\bkmkend d}}}}{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 SET i }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "MON" 0 }{\field{\*\fldinst {
\lang1036\langfe1033\langnp1036 IF d = "SUN" 1 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "SAT" 2 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "FRI" 3 }{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "THU" 4 }
{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 IF d = "WED" 5 6}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 5}}}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 {\*\bkmkstart i}5{\*\bkmkend i}}}}
{\field{\*\fldinst {\lang1036\langfe1033\langnp1036 =}{\field{\*\fldinst ref {\lang1036\langfe1033\langnp1036 c \\@YYYYMMDD }}{\fldrslt {\lang1036\langfe1033\langnp1036 20250101}}}{\lang1036\langfe1033\langnp1036 +i \\#0000'-'00'-'00}}{\fldrslt {
\lang1024\langfe1024\noproof\langnp1036 2025-01-06}}}{\lang1036\langfe1033\langnp1036 " }{\field\flddirty{\*\fldinst ref {\lang1036\langfe1033\langnp1036 b}}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 {\*\bkmkstart e}2024-12-02{\*\bkmkend e}}}}
}{\fldrslt {\lang1024\langfe1024\noproof\langnp1036 2025-01-06}}}{\lang1036\langfe1033\langnp1036  \\@DD/MM/YYYY}}{\fldrslt {\lang1036\langfe1033\langnp1036 06/01/2025}}}{\lang1036\langfe1033\langnp1036 
\par }}

Upvotes: 1

Related Questions