Text Form Calculations

J

Jose Cabral

I have two Date Text forms in a template e.g 01/10/1970 and 07/10/2005. I
need to create a box or test form and calculate the difference of these two
text e.g 30.5 years. Can this be done? I need help in this one.
 
G

Greg Maxey

Something like this:

Sub TimeSpanCalc()
Dim oFF As FormFields
Dim d1 As Date
Dim d2 As Date
Dim bLeapYear As Boolean
Dim Years As Long
Dim Months As Long
Dim Days As Long
Dim DaysInMonth As Long
Dim DoDs As String
Dim MorMs As String
Dim YorYs As String
Set oFF = ActiveDocument.FormFields
d1 = oFF("Text1").Result
d2 = oFF("Text2").Result
If Month(d2) = 2 Then
'Calculate Leap Years
bLeapYear = ((Year(d2) Mod 4 = 0) + (Year(d2) Mod 400 = 0) -
(Year(d2) Mod 100 = 0))
'Adjust days in month for Leap Years
DaysInMonth = 28 - bLeapYear
Else
DaysInMonth = 31 - (Month(d2) = 4) - (Month(d2) = 6) - _
(Month(d2) = 9) - (Month(d2) = 11)
End If
Years = Year(d2) - Year(d1) + (Month(d2) < Month(d1)) + _
(Month(d2) = Month(d1)) * (Day(d2) < Day(d1))
Months = (12 + Month(d2) - Month(d1) + (Day(d2) < Day(d1))) Mod 12
Days = (DaysInMonth + Day(d2) - Day(d1)) Mod DaysInMonth
If Days = 1 Then DoDs = "day" Else DoDs = "days"
If Months = 1 Then MorMs = "month" Else MorMs = "months"
If Years = 1 Then YorYs = "year" Else YorYs = "years"
oFF("Text3").Result = "The time span is " & Years & " " & YorYs & " "
& Months & _
" " & MorMs & " and " & Days & " " & DoDs & "."
End Sub
 
P

Peter Jamieson

How are you inserting these dates into your template? It would help if you
could say what you are tryiing to achieve.

What do you mean by "Date Text forms" ? (e.g. If you are using a non-English
language version of Word, what menus and options are you using to insert
them?)

When you say 30.5 years, how accurately do you need to calculate the
difference?

Peter Jamieson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top