date field generates date plus 30 days

B

bobotie

I'm creating a form which will generate two letters. The first letter needs
a date field and the second letter needs a calculated date field of first
letter date plus 30 days. I found this: Selection.InsertBefore
Format(((Date+30), "MMMM d yyy")
This only worked the first time we tested, the 30 day date never updated.
Thanks.
 
C

Charles Kenyon

If you need a date field, create the sucker and save it as an AutoText
entry, don't try inserting it using code. The field itself is more complex
than you might imagine, but it can be done. See
http://addbalance.com/word/datefields2.htm for information on the different
kinds of ways to make a date calculation work. It includes links to pages
with fields and an explanation of different macros that can be used instead
of fields. You can download document with a field that will work with a 30
day setting from
http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=wrd&Number=249902.
Again, writing code to create such a field is _way_ beyond my skills.

For example, a field for CreateDate plus 14 days is:
{ QUOTE "{ SET " Delay" "14" }
{ SET "DaysInMonth" { IF { CreateDate \@ "MM" } <> 2
{ = ROUND(30.575*{ CreateDate \@ "MM" },0)-
ROUND(30.575*{ = { CreateDate \@ "MM" } -1 },0) }
{ IF { = MOD( { CreateDate \@"yy" } , 4 ) } > 0 "28" "29" } } }
{ SET "NextMonth" { IF { CreateDate \@ "MM" } = 12 "1/97"
"{ = { CreateDate \@ "MM" } + 1 }/97 } }
{ IF { = { REF "Delay" } + { CreateDate \@ "dd" } } <= {
REF"DaysInMonth" }
{ CreateDate \@ "MMMM { = { REF "Delay" } + { CreateDate \@ "dd" } },
yyyy"}{ QUOTE { NextMonth \@ "MMMM" }
{ = { REF "Delay" } + { CreateDate \@ "dd" } - { REF"DaysInMonth" } },
{ IF { CreateDate \@ "MM" } <> 12 { CreateDate \@ "yyyy" }
{ CreateDate \@ "{ = 1 + { CreateDate \@ "yyyy" } \# "xxxx" }" } } } }" }

Note, you said you wanted a field. If what you want is code, you can put
code in an AutoNew that will save such a date, formatted the way you want,
and save it as either a custom document property or a custom document
variable and use a field to display that property/variable. Using vba for
date calculations is far simpler than fields. Both are addressed at the
first page linked above.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
M

macropod

Hi bobotie

The code you've posted is from a macro, which would have to be fleshed out a
bit more to work:
Sub InsertDate()
Selection.InsertBefore Format(Date + 30, "MMMM d yyyy")
End Sub
You'd also need to tell it where the insertion point is, otherwise it'll
just insert the result wherever the cursor happens to be. Plus, to get it to
work from a formfield, you'd have to set the formfield's properties to run
the macro on exit, and protect the form.

An alternative approach is to set the formfield's properties to calculate on
exit, and use a formula field to calculate the date. To see how to do this
calculation, and just about everything else you might want to do with dates
in Word, check out my Date Calc 'tutorial', at:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
There you'll find examples that you can simply copy & paste into your
document, after which all you'll need to do is to adjust the 'Delay' value.

Cheers
 
B

bobotie

Charles
I finally found the time to try your formula. The only change i made was
SET "Delay" "30" instead of 14.
Two things occurred:
1. there is no space between the month "MMMM" and "dd" i.e. December50, 2005
- how do i fix that
2. The create date is December 20, 2005 and the calculated date is December
50, 2005. What do I need to do to fix that?
Your help is most appreciated
bobotie
 
G

Graham Mayor

If you had copied the relevant section from the link posted by Charles
and simply changed the delay, you would not have let the typos slip in that
screwed up your calculation. Try the following, but copy it from the linked
document at
http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=wrd&Number=249902 and
also change the final mask to give the required date layout thus:

{QUOTE{SET Delay 30}{SET a{=INT((14-{DATE \@ M})/12)}}{SET b{={DATE \@
yyyy}+4800-a}}{SET c{={DATE \@ M}+12*a-3}}{SET d{DATE \@ d}}{SET
jd{=d+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045+Delay}}{SET
e{=INT((4*(jd+32044)+3)/146097)}}{SET f{=jd+32044-INT(146097*e/4)}}{SET
g{=INT((4*f+3)/1461)}}{SET h{=f-INT(1461*g/4)}}{SET
i{=INT((5*h+2)/153)}}{SET dd{=h-INT((153*i+2)/5)+1}}{SET
mm{=i+3-12*INT(i/10)}}{SET yy{=100*e+g-4800+INT(i/10)}}{=dd*10^6+mm*10^4+yy
\# "00'-'00'-'0000"} \@ "MMMM d, yyyy"}

Which today produces - January 20, 2006

If it is for inclusion in a template, change the DATE fields to CREATEDATE
fields (you can use Replace for that) so that the dates in the documents
created from it don't change.

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


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

macropod

Hi Graham,

Charles' field code also doesn't work reliably for delays of more than 28
days ... try it with a delay of 30 days on 30 & 31 January, and any time in
February, and you'll see what I mean. Charles mentions this limitation too
on his web site.

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