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.

ShumsExcel & VBA ExpertCommented:
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.
ShumsExcel & VBA ExpertCommented:
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

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}
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

ShumsExcel & VBA ExpertCommented:
If you already have Data Field as above then you just run Macro CalcAge
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"
ShumsExcel & VBA ExpertCommented:
Please upload your sample document.
K TinAnalystAuthor Commented:
Uploaded
test.docm
ShumsExcel & VBA ExpertCommented:
There is problem in your template. Let me make new one. Give me some time
ShumsExcel & VBA ExpertCommented:
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
ShumsExcel & VBA ExpertCommented:
Kindly note you need to double click on DOB Field and change the date in Default Date then run macro
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.