Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date formula in a Word field?

Status
Not open for further replies.

max17

Technical User
Jul 17, 2002
11
CA
Hi,

I have a document template that automatically fills in the current date for a field in the document. I would like another field to display the date that is three days before that date (i.e.: the current date minus three days). Is there a way to do this automatically? I would prefer not to have to use an Excel file instead for my template.

Any help would be much appreciated.

Thanks.

Max
 
I'm assuming since you mention the current date is put in a 'Field' that you are using form fields. If this is correct, you should be able to write a macro that uses this DateAdd(d, -3, date). DateAdd is a function that can change a specified date. The 'd' indicates to change the day rather than the year or the month, the '-3' indicates go back 3 days and date indicates today's date. Hope that helps. Leslie
landrews@metrocourt.state.nm.us

There are 10 types of people in the world -
those who understand binary
and
those who don't!
 
Thanks Leslie.

Actually, I wasn't using a form field but I am now.

I changed the document to a template, created a macro, and added added a form field to the template. When I try to use it, I get a syntax compile error for the line:
" DateAdd(d, -3, Date) "

Do you know what I'm doing wrong?

(Word2002)

Max
 
The d in DateAdd(d, -3, date) is supposed to be a string expression. So either declare it and use the variable, or put it in as "d".
 
My bad! Hope that it works now, Max! Leslie
landrews@metrocourt.state.nm.us

There are 10 types of people in the world -
those who understand binary
and
those who don't!
 
Thanks Fumei and Leslie.

Unfortunately, I'm still not getting it. Whether I try it as a regular field or as a form field, whether I use a macro or not, and whether I use d or "d", I still get a syntax error and no visible date.

I am obviously missing some crucial step(s) that may be so obvious that they have not been mentioned in the instructions I have seen so far.

Any other hints or another way to display a calculated date in a Word document?

Max
 
A miracle has happened!

I finally managed to do it with help from:

It worked to take my template, open the VB editor, choose the ThisDocument for my template, create a Document_Open(), and add the lines:

Selection.GoTo What:=wdGoToBookmark, Name:="PrevFriday"
Selection.InsertBefore Format((Date - 3), "MMMM d, yyyy")

(I plan to only use the document on Mondays so that is why the bookmark is named PrevFriday.)

I then saved the template and then a document based on the template.

Every time I open the document, the calculated date is inserted. I then either save the new instance of the document to a new name or discard the current instance. (If one saves the newly opened document, two dates will be at the bookmarked location in the document the next time it is opened.)

Thanks again Fumei and Leslie for your help.

Max
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top