Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

Setting the date in a new document...

 
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word programming
View previous topic :: View next topic  
Author Message
Cartoper
Guest





PostPosted: Mon Dec 10, 2007 2:05 pm    Post subject: Setting the date in a new document... Reply with quote

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
Back to top
Google
Sponsor





PostPosted: Mon Dec 10, 2007 2:05 pm    Post subject: Advertisement

Back to top
Graham Mayor
Guest





PostPosted: Mon Dec 10, 2007 3:06 pm    Post subject: Re: Setting the date in a new document... Reply with quote

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Cartoper wrote:
Quote:
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
Back to top
macropod
Guest





PostPosted: Tue Dec 11, 2007 1:33 am    Post subject: Re: Setting the date in a new document... Reply with quote

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
or
http://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}}

Cheers
--
macropod
[MVP - Microsoft Word]
-------------------------

"Cartoper" <cartoper@gmail.com> wrote in message news:b28f1aeb-70da-4cb5-ab97-7bf1256457b4@e10g2000prf.googlegroups.com...
Quote:
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
Back to top
Cartoper
Guest





PostPosted: Tue Dec 11, 2007 2:34 pm    Post subject: Re: Setting the date in a new document... Reply with quote

On Dec 10, 8:33 pm, "macropod" <inva...@invalid.invalid> wrote:
Quote:
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
Back to top
macropod
Guest





PostPosted: Tue Dec 11, 2007 7:40 pm    Post subject: Re: Setting the date in a new document... Reply with quote

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
--
macropod
[MVP - Microsoft Word]
-------------------------

Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum