Setting the date in a new document...

C

Cartoper

Yet again, the format of my weekly report has changed. It is a simple
Word document, one of the things I need to fill out though is the
"week ending", which is always the previous Friday.

I am going to create a template and use that as the foundation of my
weekly reports. I am assuming that with a bit of VBA code in the
template the week ending can be auto populated for me. The question
is: How do I do it? I have many years of C++ and .Net development
under my belt, but no experence in coding within Word.

Cartoper
 
G

Graham Mayor

I am sure someone will come up with something simpler, but

Dim TestDate As String
Dim ReqDate As String
Dim i As Long
For i = 0 To 6
TestDate = Format(DateAdd("d", i, Now), "ddd")
If UCase(TestDate) = "FRI" Then
ReqDate = Format((DateAdd("d", i, Now) - 7), "d MMM yyyy")
MsgBox ReqDate
End If
Next

should do the trick. Basically I have incremented the current date by one at
a time until Friday. Then subtract 7 days to give the previous Friday stored
as ReqDate

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


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

macropod

C

Cartoper

To see how to do this and just about everything else you might want to do with dates in Word, using field coding instead of vba,
check out my Date Calc 'tutorial', at:http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
orhttp://www.gmayor.com/downloads.htm#Third_party
In particular, look at the item titled 'Calculate a Stepped Date' and change the lines:
{SET Weekday 6}
and:
{SET jd{=INT((jd)/7)*7+Weekday}}
to:
{SET Weekday 4}
and
{SET jd{=INT((jd)/7-1)*7+Weekday}}

macropod,

That is a great document, thank you.

My problem is, not every having worked with Word macro's before, I
don't fully under the implementation. I understand what you are doing
above by setting the Julian day to the target day of last Friday, but:

1: How do I display it? With simply {DATE}
2: How to I set it up so that it is set when the document is created
from the template?

Cartoper
 
M

macropod

Hi Cartoper,

As mentioned in my previous post, this is not vba - it's field coding.

If you:
.. copy and paste the example from the document you downloaded to your template
.. select the filed and press Alt-F9 to expose the coding
you can make the changes I suggested.

Since this is for a template, you'll need to change all occurrences of 'DATE' in the field to 'CREATEDATE'. When you're done, press
Alt-F9 again to toggle the filed code display back to the 'normal' view and press F9 to update the field. For now, the result will
show last Friday but, whenever you create & save a new document based on the template, it'll update to the most recent Friday and
will retain that date unless you later save the file using File|Save As..

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