Calculate age in Microsoft Word

K Tin
K Tin used Ask the Experts™
on
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).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
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.
Managing Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
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
K TinAnalyst

Author

Commented:
Sorry I'm dense... so do I click Ctrl-F9 and name, within the brackets, {CalcAge} or {MACROBUTTON CalcAge}
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
If you already have Data Field as above then you just run Macro CalcAge
K TinAnalyst

Author

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"
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Please upload your sample document.
K TinAnalyst

Author

Commented:
Uploaded
test.docm
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
There is problem in your template. Let me make new one. Give me some time
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
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
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
Kindly note you need to double click on DOB Field and change the date in Default Date then run macro

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial