Using DateDiff in Formfields...

B

Bill Foley

Hey Gang,

Word 2003

Trying to create an automated form for my church and am stumped as to how to
do this. I have a calendar form (actually two of them) that populate
formfields (StartDate and EndDate). I have another formfield (TotalDays)
that I cm trying to populate with the number of days difference between the
other two formfields. I assigned the following macro to the "Entry" of the
TotalDays field. However, it is not working.

Sub CalculateTotalDays()
ActiveDocument.FormFields("TotalDays").Result = DateDiff("d", EndDate,
StartDate)
End Sub

I am assuming that Word can't recognize formfields the same way Acces does
to know to use them in the calculation, but I can't seem to find a way to
make it work.

Any ideas? TIA!
 
G

Graham Mayor

If you don't mind Jezebel's unwarranted scorn, you could do this with a Word
field assembly as opposed to a form field for the result. The construction
is :

{={SET a{=INT((14-{EndDate \@ M})/12)}}{SET b{={EndDate \@
yyyy}+4800-a}}{SET c{={EndDate \@ M}+12*a-3}}{SET d{EndDate \@
d}}{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}-{SET
a{=INT((14-{StartDate \@ M})/12)}}{SET b{={StartDate \@ yyyy}+4800-a}}{SET
c{={StartDate \@ M}+12*a-3}}{SET d{StartDate \@
d}}{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}\# ,0}

www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902 will give you the
source document containing the fields to avoid trying to reconstruct from
here. Don't forget to check the calculate on exit check boxes of the two
date source fields.


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Bill Foley

Thanks, Graham. I'll give it a go. I'll also hang around for the "correct"
way from Jezebel! HA!
 
D

Doug Robbins - Word MVP

ActiveDocument.FormFields("TotalDays").Result = DateDiff("d", EndDate,
StartDate)

Should be:

ActiveDocument.FormFields("TotalDays").Result = DateDiff("d",
ActiveDocument.FormFields("EndDate").Result,
ActiveDocument.FormFields("StartDate").Result)



--
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
 
G

Greg Maxey

Bill,

I won't enter the fray and say which is more correct (I don't know). I
did it with a macro like this:
Sub CalculateTotalDays()
Dim oFF As FormFields
Dim EndDate As Date
Dim StartDate As Date
Set oFF = ActiveDocument.FormFields
EndDate = oFF("EndDate").Result
StartDate = oFF("StartDate").Result
oFF("TotalDays").Result = DateDiff("d", EndDate, StartDate)
End Sub
 
B

Bill Foley

Kept getting some sort of "syntax error" so I gave into the other way. Must
not be smart enough to use the "official field" method! HA!

THANKS!
 
G

Graham Mayor

Curious - as the default settings in the master document even have the same
field names. It works for me straight out of the box by simply cutting and
pasting the code. the problem may have been that of having the calculate on
exit checkbox checked on both fields as I suggested, rather than on the
second only as there would be no date in the second field when starting and
that would confuse it :)

However our friend Jezebel will no doubt have a wry smile on his face ;)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

macropod

Hi Bill,

If you used the field coding Graham posted, you need to ensure all the field
braces (i.e. '{ }') are created in pairs via Ctrl-F9. Simply typing them
won't work and will produce syntax errors just like you described.

Cheers
 
G

Graham Mayor

I did suggest he pasted the original from your source document which will
work as supplied - even to the correct field names.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

macropod

Hi Graham,

Yes, I noticed that. Not doing so and messing up the coding is the only
reason I can think of for the syntax error.

Cheers
 

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