Forum Discussion

Andreas1991's avatar
Andreas1991
Copper Contributor
Jun 04, 2024

Automatic Age Calculation in Word Document

Hi everyone, 

I am writing a Business Plan and I'd like to insert a person's age automatically as I don't know when this document will be needed. Therefore I just want to make the document "smarter" by automating some things hence don't worry about updating it in x years when it is needed. 

 

After a couple of hours of research now and endless attempts to do so I reached the end of my patience. The last thing I did was working with fields and inserting formulars I found online. 

Here is what I have written in a field (by pressing CTRL+F9): {=INT ({DATE\@"yyyy"} + {DATE\@"M"}/12 + {DATE\@"d"}/365.25-1989-8/12-25/365.25)}

Here you can see that I want to calculate the age today of this person (DOB: 25 August 1989).

 

Any help here is greatly appreciated. Not sure if it is better to work with fields here or macros. Happy for every solution which is "easy" to implement 🙂

5 Replies

  • Hugh_B's avatar
    Hugh_B
    Copper Contributor

    I have a calculation that calculates age in an Excel spredsheet. I can find and post on here if you think it will be of use? It is based on recorded date of birth!

  • Charles_Kenyon's avatar
    Charles_Kenyon
    Bronze Contributor

    You could use a REF field that would take you to a bookmark containing someone's date of birth. That would replace the ASK field in the complex field I gave earlier. I think that fields will be simpler long-term. No method will be completely automatic.

    Again, calculations in Excel are far easier.

    You may be better keeping the base data in an Excel spreadsheet and using MailMerge to bring values and information into a Word document. This could be done through a catalogue/directory mailmerge if you want or a regular letter mailmerge creating a new document.

     

  • cdrgreg's avatar
    cdrgreg
    Copper Contributor

    It is pretty simple with a macro:

    Sub TestDetailed()
      'MsgBox fcnCalcAge(InputBox("Enter birth date in ""MM/dd/yyyy"" formmat", "Birth Date"), Now)
      MsgBox fcnCalcAge("12/31/1958 4:34", Now)
    End Sub

    Function fcnCalcAge(oDateBD As Date, oDateNow As Date, Optional ShowAll As Boolean = False, Optional Grammar As Boolean = True)
    Dim lngYear As Long, lngMonth As Long, lngDay As Long
    Dim oDateLDPM As Date, oDateLDIM As Date, oAnchorDate As Date
         
      'Restrict to date only (i.e., no 12/31/1958 3:15 p.m.)
      oDateBD = Int(oDateBD)
      oDateNow = Int(oDateNow)
      'Limit to to persons already borned.
      If oDateBD > oDateNow Then
        fcnCalcAge = "Come back after your birth date."
        Exit Function
      End If
      'Calculate complete years passed.
      If Year(oDateNow) > Year(oDateBD) Then
        'A different calendar year. Has one or more complete years passed?
        If Month(oDateNow) = Month(oDateBD) Then
          'Same month in subsequent year. Check day.
          If Day(oDateNow) >= Day(oDateBD) Then
            'Complete year passed
            lngYear = DateDiff("yyyy", oDateBD, oDateNow)
          Else
            'e.g, birthdate 6/14/2000, date now 6/13/2015 returns fourteen years.
            lngYear = DateDiff("yyyy", oDateBD, oDateNow) - 1
          End If
        ElseIf Month(oDateNow) > Month(oDateBD) Then
          'Complete year passed.
          lngYear = DateDiff("yyyy", oDateBD, oDateNow)
        Else
          'e.g, birthdate 7/13/2000, date now 6/13/2015 returns fourteen years.
          lngYear = DateDiff("yyyy", oDateBD, oDateNow) - 1
        End If
      Else
        'Obviously no complete year passed.
        lngYear = 0
      End If
      'Calculate full months passed from last full year.
      lngMonth = (DateDiff("m", DateSerial(Year(oDateBD), Month(oDateBD), 1), _
                  DateSerial(Year(oDateNow), Month(oDateNow), 1)) + IIf(Day(oDateNow) >= Day(oDateBD), 0, -1)) Mod 12
      'Calculate number of days passed from last full month.
      If Day(oDateNow) >= Day(oDateBD) Then
        lngDay = Day(oDateNow) - Day(oDateBD)
      Else
        'Calculate for end of month.
        'Get date on last day of previous month.
        oDateLDPM = DateSerial(Year(oDateNow), Month(oDateNow), 0)
        'Get date on last day of index month.
        oDateLDIM = DateSerial(Year(oDateNow), Month(oDateNow) + 1, 0)
        oAnchorDate = DateSerial(Year(oDateNow), Month(oDateNow) - 1, Day(oDateBD))
        If oDateLDIM = oDateNow Then
          If lngMonth = 11 Then
            'Reset month and add a year.
            lngMonth = 0
            lngYear = lngYear + 1
          Else
            lngMonth = lngMonth + 1
          End If
        Else
          lngDay = DateDiff("d", IIf(oAnchorDate > oDateLDPM, oDateLDPM, oAnchorDate), oDateNow)
        End If
      End If
      If lngYear >= 1 Then
        fcnCalcAge = lngYear & IIf(lngYear = 1, " year, ", " years, ") & lngMonth & IIf(lngMonth = 1, " month, ", _
                     " months, ") & lngDay & IIf(lngDay = 1, " day", " days")
      Else
        If lngMonth >= 1 Then
           fcnCalcAge = lngMonth & IIf(lngMonth = 1, " month, ", " months, ") & lngDay & IIf(lngDay = 1, " day", " days")
        Else
           fcnCalcAge = lngDay & IIf(lngDay = 1, " day", " days")
        End If
      End If
    lbl_Exit:
      Exit Function
    End Function

  • Samthroy09's avatar
    Samthroy09
    Copper Contributor

    Accurately knowing your age is easy with modern online tools. By entering your date of birth, you can instantly calculate your age in years, months, days, or even minutes—saving time and ensuring precision for personal or professional needs. Visit https://agecalculationar.com/

  • Charles_Kenyon's avatar
    Charles_Kenyon
    Bronze Contributor

    Andreas1991

    Date calculations in Word fields are incredibly complex. Excel is much simpler for this.

    Calculated Dates in Microsoft Word using Fields or vba

     

    Download Word MVP Paul Edstein's Microsoft Word Date Calculation Tutorial .

    It is a document with multiple Date calculation fields that you can copy and adapt in your templates.

    Read the introductory remarks.

    An alternative download location is Downloads from Graham Mayor, MVP under third-party downloads.

     

    You could also use the Insert Future (or Past) Date Add-in by Graham Mayor, MVP 

     

    The following is from page 11 of the tutorial document. Do NOT try to type this field. Copy it from the tutorial and modify to suit.

     

    "The following field uses an ASK field to get your birth date, from which to calculate one’s age:

    If your Date of Birth was 1 January 1901, then your age is 123 Years, 5 Months and 3 Days.

     

    {QUOTE
    {ASK BirthDate "What is the Birthdate?"}
    {SET by {BirthDate \@ yyyy}}
    {SET bm {BirthDate \@ M}}
    {SET bd {BirthDate \@ d}}
    {SET yy {DATE \@ yyyy}}
    {SET mm {DATE \@ M}}
    {SET dd {DATE \@ d}}
    {SET md{=IF((mm=2),28+(mm=2)*((MOD(yy,4)=0)+(MOD(yy,400)=0)-(MOD(yy,100)=0)),31-((mm=4)+(mm=6)+(mm=9)+(mm=11)))}}
    {Set Years{=yy-by-(mm<bm)-(mm=bm)*(dd<bd) \# 0}}
    {Set Months{=MOD(12+mm-bm-(dd<bd),12) \# 0}}
    {Set Days{=MOD(md+dd-bd,md) \# 0}}
    {IF{={IF{={BirthDate \@ "dddd"}}= "!*" 0 1}+{IF{={BirthDate \@ "MMMM"}}= "!*" 0 1}+{IF{by}= "!*" 1 0}+{IF{BirthDate \@ yyyyMMdd}<{DATE \@ yyyyMMdd} 0 1}}= 0 "If your Date of Birth was {Birthdate \@ "d MMMM yyyy"}, then your age is {Years} Year{IF{Years}= 1 "" s}, {Months} Month{IF{Months}= 1 "" s} and {Days} Day{IF{Days}= 1 "" s}." "Data entry error!"}}

Resources