Date Formula in a Field

  • Thread starter Thread starter Thomas M
  • Start date Start date
T

Thomas M

Word 2000

I have a weekly status report document that contains the following
information in the heading:

Report Period: April 4 to April 8, 2005

I'd like to insert fields into the heading so that the first date is always
for Monday of the current week, and the second date is always Friday of the
current week. I know how to insert basic fields, but I don't know how to
get this fancy will fields and formulas in Word. Any ideas?

--Tom
 
See http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=wrd&Number=249902
and http://addbalance.com/word/datefields2.htm. This is more complex than
you imagine.
--
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

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
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.
 
Thanks for the reply. Sorry that it took me so long to respond on this, but
frankly, I forgot about the issue until I opened my file today. While I
didn't know how to accomplish this in the way that I originally posed the
question, I knew enough about fields and date handling in Word to know that
it probably would not be easy.

I clicked on the first link that you posted, and the resulting article
mentioned working with macros. I don't know why I didn't think of that
before, but it occurred to me that I could write a macro to put the
necessary values into some document variables, and then insert some
formatted fields that refer to those document variables. Here's the macro
that I wrote:

Private Sub Document_Open()

Dim EndDate As Date
Dim StartDate As Date

' Calculates the StartDate and EndDate values.
StartDate = Date - (Weekday(Date) - vbMonday)
EndDate = Date - (vbFriday - Weekday(Date))

' Writes the StartDate and EndDate values to document variables that are
used in the document heading.
ActiveDocument.Variables("StartDate").Value = StartDate
ActiveDocument.Variables("EndDate").Value = EndDate

' Updates the fields in the document.
ActiveDocument.Fields.Update

End Sub

I'll probably add a check for the document variables so that if they ever
get deleted somehow, the code will recreate them instead of just crashing.
In the document I put the following:

Report Period: { DOCVARIABLE "StartDate" \*MERGEFORMAT "MMMM DD, YYYY" }
to
{ DOCVARIABLE "EndDate" \*MERGEFORMAT "MMMM DD, YYYY" }

It seems to work well, but thus far I've only tested it on a Friday, so I'm
not 100% certain that there isn't a bug in the code.

--Tom
 
The second article gives several sample macros that have been tested. There
are more on Graham Mayor's site.
--
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

See also the MVP FAQ: http://www.mvps.org/word which is awesome!
--------- --------- --------- --------- --------- ---------
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.
 
It turns out that there was an error in my code. If the current date is a
Monday, the EndDate would not calculate correctly. I believe that the
following code will work for all weekdays (but I've only tested it for
Monday thus far).

Private Sub Document_Open()

Dim EndDate As Date
Dim StartDate As Date

' Calculates the StartDate and EndDate values.
StartDate = Date - (Weekday(Date) - vbMonday)
EndDate = StartDate + 4

' Writes the StartDate and EndDate values to document variables that are
used in the document heading.
ActiveDocument.Variables("StartDate").Value = StartDate
ActiveDocument.Variables("EndDate").Value = EndDate

' Updates the fields in the document.
ActiveDocument.Fields.Update

End Sub

--Tom
 

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

Back
Top