Creating a custom date field in Word (WWYY)

229 Views Asked by At

I am trying to display WWYY in word using the excel formula TEXT(WEEKNUM(TODAY()),"00")&TEXT(TODAY(),"YY")

The purpose of the document is to display the week and year when the document was opened. Let's say the document was opened today. it should display 0823.

Basically, if I go to Insert -> Date & Time , I would like to see a custom date field there as WWYY.

I tried creating the formula in excel and linking to word, but it's not working. I would like to implement this only in word for convenience.

2

There are 2 best solutions below

0
atiqorin On

Assuming you have text as "WWYY" in your word document, the approach I mention would at least replace "WWYY" with the format you want.

You have to save the document as a Word Macro-Enabled Document (.docm). Open up Microsoft Visual Basic Editor using the shortcut Alt+F11 or Option+fn+F11 (macOS). Click on ThisDocument on the Project Toolbar on left. The Code editor for ThisDocument will open up. Now you can use similar code as below to replace Instances of WWYY with the value populated. For today's date WWYY will now be replaced with 0823. I have also written another function that will revert the value(i.e. 0823) with WWYY when the file closes. That way when the file is opened in a different time, the functionality would still work.

Beware! It will replace all the instances of WWYY or 0823. So a better idea is to use a prefix to WWYY. In document you'd have Prefix WWYY and also corresponding prefix in both RevertWWYY and ReplaceWWYY functions. (.Text = "Prefix WWYY" in ReplaceWWYY and in RevertWWYY .Text = "Prefix " + CStr(Forma... and .Replacement.Text = "Prefix WWYY").

Private Sub Document_Close()
    Call RevertWWYY
End Sub

Private Sub Document_New()

End Sub

Private Sub Document_Open()
    Call ReplaceWWYY
End Sub
 
Sub ReplaceWWYY()
With Selection.Find
     .Forward = True
     .Text = "WWYY"
     .Replacement.Text = CStr(Format(DatePart("ww", Date, vbMonday, vbFirstFourDays), "00")) + CStr(Right(Year(Date), 2))
     .Execute Replace:=wdReplaceAll, Forward:=True, _
     Wrap:=wdFindContinue
    End With
    ActiveDocument.Save
End Sub
Sub RevertWWYY()
With Selection.Find
     .Forward = True
     .Text = CStr(Format(DatePart("ww", Date, vbMonday, vbFirstFourDays), "00")) + CStr(Right(Year(Date), 2))
     .Replacement.Text = "WWYY"
     .Execute Replace:=wdReplaceAll, Forward:=True, _
     Wrap:=wdFindContinue
    End With
    ActiveDocument.Save
End Sub
3
macropod On

Word has no equivalent to Excel's WEEKDAY function. The desired result can be achieved via a field coded as:

{
{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 JDEnd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}}

{SET a 1}
{SET b{={DATE \@ YYYY}+4800-a}}
{SET c 10}
{SET d 0}
{SET JDStart{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}}

=INT((JDEnd+6-INT(JDStart/7)*7)/7) \# 00}{DATE \@ YY}

The above field code is based on the Calculate the # Days Difference Between Two Dates example in my Microsoft Word Date Calculation Tutorial, available from:

https://www.msofficeforums.com/word/38719-microsoft-word-date-calculation-tutorial.html

Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. The spaces represented in the field constructions are all required. The line breaks aren't required - they're just being used to make the code structure easier to see.

For a macro to convert the above field code representation into a working field code, see Convert Text Representations of Fields to Working Fields in the Mailmerge Tips and Tricks thread at:

https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html

For anyone who may want to simplify the field code for getting JDStart, one could reduce:

{SET a 1}
{SET b{={DATE \@ YYYY}+4800-a}}
{SET c 10}
{SET d 0}
{SET JDStart{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}}

to:

{SET b{={DATE \@ YYYY}+4799}}
{SET JDStart{=365*b+INT(b/4)-INT(b/100)+INT(b/400)-31739}}