Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Conversions / July 2003

Tip: Looking for answers? Try searching our database.

How to Calculate a person's age in a Word form template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fred Pate - 25 Jul 2003 16:08 GMT
I have a MS Word form that I use all of the time and one
of the fields is the person's date of birth. I want to be
able to use their date of birth entry to calculate their
age and have it placed into the next field in the form. I
searched the knowledgebase and was able to find out how
to do it in any program, as long as it was not Word.

I would be very grateful for any help,

Fred
Doug Robbins - Word MVP - 26 Jul 2003 06:19 GMT
Hi Fred,

If you run a macro containing the following code on exit from the FormField
Text1, and a valid date is entered into that formfield, the age in years,
months and days will be inserted into the FormField Text2:

' Macro created 16/9/00 by Doug Robbins
' To calculate the age in years, months and days
' based on a birthdate entered in Formfield Text1, the age
' will be inserted into Formfield Text2
'
Dim Years As Long
Dim Months As Long
Dim Days As Long
Dim Birthday As Date
Dim Day As Long
Dim Age As String
Dim Agedays As String
Dim Yearnow As Long
Dim Monthnow As Long
Monthnow = Val(Format(Date, "m"))
Yearnow = Val(Format(Date, "yyyy"))
Dim mon$(12)
   mon$(1) = "January": mon$(2) = "February": mon$(3) = "March": mon$(4) =
"April": mon$(5) = "May": mon$(6) = "June": mon$(7) = "July": mon$(8) =
"August": mon$(9) = "September": mon$(10) = "October": mon$(11) =
"November": mon$(12) = "December"
Dim monthdays$(12)
   If ((Yearnow Mod 4 = 0 And Calyear Mod 400 = 0) Or (Calyear Mod 4 = 0
And Calyear Mod 100 <> 0)) Then
       monthdays$(1) = "31": monthdays$(2) = "29": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"
   Else
       monthdays$(1) = "31": monthdays$(2) = "28": monthdays$(3) = "31":
monthdays$(4) = "30": monthdays$(5) = "31": monthdays$(6) = "30":
monthdays$(7) = "31": monthdays$(8) = "31": monthdays$(9) = "30":
monthdays$(10) = "31": monthdays$(11) = "30": monthdays$(12) = "31"
   End If
Birthdate = ActiveDocument.FormFields("Text1").Result
Years = DateDiff("yyyy", Birthdate, Date)
Months = DateDiff("m", Birthdate, Date) - Years * 12
Birthday = Format(Birthdate, "d")
Day = Format(Date, "d")
Days = Day - Birthday
If Days > 0 Then
   Agedays = " and " & Format(Days) & " days."
ElseIf Days < 0 Then
   Agedays = " and " & Format(monthdays$(Monthnow) - Birthday + Day) & "
days."
   Months = Months - 1
Else
   Agedays = "."
End If
Age = Format(Years) & " years, " & Format(Months) & " Months" & Agedays
ActiveDocument.FormFields("Text2").Result = Age

NOTE If you copy and paste the above code into the visual basic editor,
there will be a few problems with linebreaks that you will have to rectify.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested.  Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
> I have a MS Word form that I use all of the time and one
> of the fields is the person's date of birth. I want to be
[quoted text clipped - 6 lines]
>
> Fred
macropod - 27 Jul 2003 01:26 GMT
Hi Fred,

You can do this with a Word field - no macros required. To
see how, go to:
http://www.wopr.com/cgi-bin/w3t/showflat.pl?
Cat=&Board=wrd&Number=249902
(all one line) where you can download a Word document with
fields for doing this and lots of other date-based
calculations.

Cheers
PS: Remove NO.SPAM from the above before replying.
>-----Original Message-----
>I have a MS Word form that I use all of the time and one
[quoted text clipped - 9 lines]
>
>.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.