Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-12-2023, 09:16 AM
JohnDohe JohnDohe is offline How to use/create code in MS Word to calculate and display age in years? Windows 7 64bit How to use/create code in MS Word to calculate and display age in years? Office 2010 64bit
Novice
How to use/create code in MS Word to calculate and display age in years?
 
Join Date: Mar 2015
Posts: 7
JohnDohe is on a distinguished road
Default How to use/create code in MS Word to calculate and display age in years?

Hi!

I am not a programmer so please ... don't be too rough on me!

In an MS Word (vintage 2010) document I need to calculate age in years from a birthdate and a chosen date.

I've done a lot of searching and hope this can work:
Redirecting

{QUOTE{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 Years{=yy-by-(mm<bm)-(mm=bm)*(dd<bd) \# 0}}


Ultimate goal: to be able to click on a dropdown calendar field(not sure if this is the correct word to use!) to choose the BirthDate,
then use another dropdown calandar field(?) to enter a DATE, then have MS Word calculate the age in years between those two dates,
and only display the age in years (not the BirthDate nor the DATE) in the final document.


My problem is that the above code seems to refer to field code(s) and I have no idea how to work with them. Above my pay grade!

But I can follow directions

Please lend a guiding hand with step-by-step instructions for how to use the above code to calculate the age in years from
a birthdate and another date.

If the above code is not the "right stuff" please advise what I should use.

Thanks so much!
Reply With Quote
  #2  
Old 09-12-2023, 10:24 AM
Charles Kenyon Charles Kenyon is online now How to use/create code in MS Word to calculate and display age in years? Windows 11 How to use/create code in MS Word to calculate and display age in years? Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
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

You have cross-posted. Your link is not to the source of your field or your research, but to you asking the same question in another forum. This is apparently the third forum in which you have posted. Birthdate would refer to a bookmark named birthdate that would contain the date. That could be a bookmark around a date picker content control.

It is time for you to start learning to deal with field code. Period. You want to do something that requires a complex field, it is time to learn at least how to create, edit, and manipulate fields.

Here are directions you can follow to do that.
Here is a tutorial document with field codes for all sorts of calculations based upon dates, including the one you seek. Read the introductory material before finding the field you need. You can then find that field and copy it into your document/template. It is pinned at the top of this forum.
MVP Paul Edstein has done all the heavy lifting, but you are required to think. If you do not understand how a bookmark is created someone here will be happy to help you with that. I do not certify that you have a working field already. You seem to think it should work.

Here you can find a utility that can take text like that you have posted and convert it to a working field.
Reply With Quote
  #3  
Old 09-12-2023, 10:54 AM
Charles Kenyon Charles Kenyon is online now How to use/create code in MS Word to calculate and display age in years? Windows 11 How to use/create code in MS Word to calculate and display age in years? Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,140
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

Here is a screenshot comparing your field, as repaired by Jay Freedman, and one from the tutorial.

The date picker has a bookmark set of "Birthdate."
When a date is entered and then the fields are selected and updated (F9), the result is:


What is the birthdate? 6/20/1966

Your field
Age:

Field from tutorial with BirthDate substituted for StartDate and Date substituted for EndDate.
57 Years, 2 Months, 22 Days.

================================
Note, your field produced no result. I have no interest in trying to fix it. Jay Freedman, in your other post already corrected the matching of braces.



Here is the second field in text, which could be converted to a real field using Graham Mayor's utility. (Do not try typing it yourself unless you really like pain.)

{QUOTE{SET sy{BirthDate \@ yyyy}}{SET sm{BirthDate \@ M}}{SET sd{BirthDate \@ d}}{SET ey{Date \@ yyyy}}{SET em{Date \@ M}}{SET ed{Date \@ d}}{SET md{=IF((em=2),28+(em=2)*((MOD(ey,4)=0)+(MOD(ey,400 )=0)-(MOD(ey,100)=0)),31-((em=4)+(em=6)+(em=9)+(em=11)))}}{Set Years{=ey-sy-(em<sm)-(em=sm)*(ed<sd)}}{Set Months{=MOD(12+em-sm-(ed<sd),12) \# 0}}{Set Days{=MOD(md+ed-sd,md) \# 0}}"{Years} Year{IF{Years}= 1 "" s}, {Months} Month{IF{Months}= 1 "" s}, {Days} Day{IF{Days}= 1 "" s}."}


Here it is in text trimmed to simply produce the number of years.


{QUOTE
{SET sy{BirthDate \@ yyyy}}
{SET sm{BirthDate \@ M}}
{SET sd{BirthDate \@ d}}
{SET ey{Date \@ yyyy}}
{SET em{Date \@ M}}
{SET ed{Date \@ d}}
{SET md{=IF((em=2),28+(em=2)*((MOD(ey,4)=0)+(MOD(ey,400 )=0)-(MOD(ey,100)=0)),31-((em=4)+(em=6)+(em=9)+(em=11)))}}
{Set Years{=ey-sy-(em<sm)-(em=sm)*(ed<sd)}}
"{Years}"}

Last edited by Charles Kenyon; 09-16-2023 at 10:17 AM.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Create report for when a date is 3 years old Daronovitz Excel 5 11-01-2022 02:42 PM
Word 2016: How do I change a REF field code to display a reference to a Roman number as Arabic Peggy Sue Word 1 08-15-2017 09:50 PM
How to use/create code in MS Word to calculate and display age in years? Calculate average age (years) lyiio Excel 3 08-13-2017 12:12 AM
Is this possible? - Calculate and Display the date range between 2 specific dates Premier-UK Excel 2 08-18-2014 05:43 AM
How to use/create code in MS Word to calculate and display age in years? Using Field Codes to Calculate 5 years from a Date vandy Word 1 09-23-2009 11:32 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:07 AM.


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