How do I calculate a duration of time in Word using form fields?

B

Barbara Nie

I have three form fields that are bookmarked ... [start_date], [stop_date]
and [duration_months].

Both dates are text form fields of "Date" type and formatted "mm/yy".

[Duration_months] is a text form field of "Calculation" type with the
expression
"= datedif [stop] [start],m", length "Unlimited" and number format "0".

The [Duration_months] field is returning the character "t".

Example: [start_date] = 02/05 [stop_date] = 10/05
[duration_months] should come up with 8

Any help would be greatly appreciated!!
 
D

Doug Robbins

You will need to use some VBA code in a macro run on exit from the second
formfield to do this.

The following calculates the duration between two formfields containing
times, but it should give you an idea how to achieve what you are after:

' a Macro to calculate the elapsed time for formfields with Date format of
HH:mm

' Macro created 16 May 1999 by Doug Robbins - Word MVP

'

Start = ActiveDocument.FormFields("Text1").Result

StartHour = Val(Left(Start, 2))

StartMinutes = Val(Right(Start, 2))

StartTimeMinutes = StartHour * 60 + StartMinutes

Finish = ActiveDocument.FormFields("Text2").Result

FinishHour = Val(Left(Finish, 2))

FinishMinutes = Val(Right(Finish, 2))

FinishTimeMinutes = FinishHour * 60 + FinishMinutes

ElapsedMinutes = FinishTimeMinutes - StartTimeMinutes

ElapsedHours = Int(ElapsedMinutes / 60)

ElapsedMinutes = ElapsedMinutes - ElapsedHours * 60

ActiveDocument.FormFields("Text3").Result = Str(ElapsedHours) & ":" &
Format(ElapsedMinutes, "00")


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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