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.
Greg Maxey - 07 Mar 2007 03:34 GMT
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
On Mar 6, 5:24 pm, Jose Cabral <JoseCab...@discussions.microsoft.com>
wrote:
> 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.
macropod - 07 Mar 2007 06:00 GMT
Hi Jose,
To see how to do this and just about everything else you might want to do with dates in Word using fields instead of vba, check out
my Date Calc 'tutorial', at:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
In particular, look at the item titled 'Calculate the # Years Months & Days Difference Between Two Dates'
Cheers

Signature
macropod
[MVP - Microsoft Word]
-------------------------
>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.
Peter Jamieson - 07 Mar 2007 18:14 GMT
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
>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.