Calculate age in Microsoft Word

I am wanting to calculate age on a form document in word. I just need a easy to follow macro (I'm a newb to this).
K TinAnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsDistinguished Expert - 2017Commented:
I am not sure which form field you have, you'll need to do the work yourself. To help you get started, check below link:
How Can I Put Calculated Dates in Word?

Take a look and let me know what additional questions you have.
0
ShumsDistinguished Expert - 2017Commented:
Assuming that you are using FormFields in a document that is protected for forms and that the bookmark name DOB is assigned to the formfield into which the date of birth is to be inserted and the bookmark name of Age is assigned to the formfield in which the age is to be displayed, running a macro containing the following code on exit from the DOB formfield will populate the Age formfield with the age in years.
Public Sub CalcAge()
Dim objAge As Word.FormField
Dim objBirthDate As Word.FormField
Dim datToday As Date
Dim intAge As Integer
Dim intYears As Integer

datToday = Date
Set objAge = ActiveDocument.FormFields("Age")
Set objBirthDate = ActiveDocument.FormFields("DOB")

    If IsDate(objBirthDate.Result) = False Then
      objAge.Result = ""
      Set objAge = Nothing
      Set objBirthDate = Nothing
    Else
        intYears = DateDiff("yyyy", objBirthDate.Result, datToday)
        If intYears > 0 Then
            intAge = intYears - Abs(DateDiff("d", datToday, DateAdd("yyyy", intYears, objBirthDate.Result)) > 0)
        End If
        objAge.Result = intAge
    End If

End Sub

Open in new window

Hope this helps
I tweaked the code from Calculating age from a DOB field
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
K TinAnalystAuthor Commented:
Sorry I'm dense... so do I click Ctrl-F9 and name, within the brackets, {CalcAge} or {MACROBUTTON CalcAge}
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ShumsDistinguished Expert - 2017Commented:
If you already have Data Field as above then you just run Macro CalcAge
0
K TinAnalystAuthor Commented:
When I try to run the Macro I get an error "Run time error 5941 the requested member of the collection does not exist"
0
ShumsDistinguished Expert - 2017Commented:
Please upload your sample document.
0
K TinAnalystAuthor Commented:
Uploaded
test.docm
0
ShumsDistinguished Expert - 2017Commented:
There is problem in your template. Let me make new one. Give me some time
0
ShumsDistinguished Expert - 2017Commented:
Please find attached corrected one. You haven't named the bookmark for your field. Change the date in Date Of Birth Field and run macro CalcAge, it will display numbers of years in Age Field.
test_v2.docm
0
ShumsDistinguished Expert - 2017Commented:
Kindly note you need to double click on DOB Field and change the date in Default Date then run macro
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.