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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 Header with date 3

Status
Not open for further replies.

firefytr

Technical User
May 18, 2004
1,130
US
Hello,

Can't find the answer, so I thought I'd ask here. I have a word 2003 document with a header that has a table in it. The table has two columns and one row. I have a date in the right column. I need this date to automatically populate with the third Wednesday of the month - that is the month that it was opened in.

This document will only be opened once per month and this minute detail would be better off being updated automatically for me rather than trying to remember it (which I inevitably will not).

Is this too difficult? If you need more info, just let me know.

-----------
Regards,
Zack Barresse
 


Zack,

Only way I know is via code...
Code:
Function ThirdWed(dDte As Date) As Date
    ThirdWed = DateSerial(Year(dDte), Month(dDte), 1) + 14
    Do Until ThirdWed Mod 7 = 4
        ThirdWed = ThirdWed + 1
    Loop
End Function
Just pass the current date.

Skip,

[glasses] [red]Be Advised![/red] The only distinction between a bird with one wing and a bird with two, is merely...
a difference of A Pinion! [tongue]
 
Hi Zak,

Here's a field code solution, coded for systems using dd/mm/yyyy in their regional settings:
Code:
{QUOTE
{SET a{=INT((14-MOD({DATE \@ M},12)+1)/12)}}
{SET b{={DATE \@ yyyy}+4800-a}}
{SET c{=(MOD({DATE \@ M},12)+1)+12*a-3}}
{SET jd{=1+INT((153*c+2)/5)+365*b+INT(b/4)-INT(b/100)+INT(b/400)-32045}}
{SET Weekday 3}
{SET Week 3}
{SET Limit{=IF(({DATE \@ M}=1)*((MOD(yy,4)=0)+(MOD(yy,400)=0)-(MOD(yy,100)=0))=1,29,IF(({DATE \@ M}=1,28,IF(({DATE \@ M}=3)+({DATE \@ M}=6)+({DATE \@ M}=8)+({DATE \@ M}=10)=1,30,31))}}
{SET jd{=INT(jd/7+MAX(Week-1,0))*7+Weekday+(INT(jd/7)*7+Weekday<jd)*7-((INT(jd/7+Week-1)*7+Weekday+(INT(jd/7)*7+Weekday<jd)*7>jd+Limit-1)*INT((INT(jd/7+Week-1)*7+Weekday+(INT(jd/7)*7+Weekday<jd)*7-(jd+Limit))/7+1)*7)}}
{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"} \@ "dddd, d MMMM yyyy"}
There are two variables in the code to allow you to set the date to whichever week and day combination you want. The value in {SET Weekday 3} determines the day of the week to return (0 = Mon, 1 = Tues, 2 = Wed, 3 = Thurs, 4 = Fri, 5 = Sat, 6 = Sun), while the value in {SET Week 3} allows you to specify the week of the month.

The code also uses a ‘Limit’ variable to prevent the chosen date rolling over into the following month. Although not necessary for your application, this also means that setting the Week variable to 5 will always return the last instance of a given weekday occurring in a month.

If you'd rather not have to code all this yourself, you can download my Date Calc 'tutorial' at:
and copy the field from there - it's under the heading 'Calculate the day & date of a given day of the week next month'. All you'll need to do is to adjust the above parameters to suit your requirements. The 'tutorial' also has instructions on how to modify the last line of the code for use with mm/dd/yyyy regional settings.

Cheers

[MS MVP - Word]
 
Hi Skip,

Thanks for the function. That would seem the first step of this process to me. (I'm an Excel geek, I know very little of Word.) Do you know the code to update the header? I'm unsure of the correct syntax.

-----------
Regards,
Zack Barresse
 



I'm no Word expert either, but give this a try.
Code:
Sub Macro1()
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection = ThirdWed(Date)
End Sub

Skip,

[glasses] [red]Be Advised![/red] The only distinction between a bird with one wing and a bird with two, is merely...
a difference of A Pinion! [tongue]
 
macropod,

Thanks, but I'm not sure how to use that code you gave me.

Skip, I'll try yours as well. Will let ya'll know how it turns out.

Thanks very much you two! :)

-----------
Regards,
Zack Barresse
 
With all due respect macropod - and I know fields DO work well, and sometimes better - that is precisely why I prefer VBA for a lot of things. Back when VBA really sucked fields were the best way of doing things. Now?

As for getting this to work on opening. Put the following into the ThisDocument code module.
Code:
Function ThirdWed(dDte As Date) As Date
    ThirdWed = DateSerial(Year(dDte), Month(dDte), 1) + 14
    Do Until ThirdWed Mod 7 = 4
        ThirdWed = ThirdWed + 1
    Loop
End Function

Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Tables(1)
        .Cell(1, 2).Range.Text = ThirdWed(Now)
End With
End Sub
This is on the assumption that the header IS Primary, and it is in Section 1. Adjust accordingly. Every time you open the document the header date will be adjusted to the third Wednesday of the current month.

Gerry
 
That is, adjusted automatically. You will not have to do anything. Of course if you want manual control of this that is another thing, but just as easy to do.

Gerry
 


Zack,

Gerry is the emerging Word GURU here! I kinda stumble around.

Skip,

[glasses] [red]Be Advised![/red] The only distinction between a bird with one wing and a bird with two, is merely...
a difference of A Pinion! [tongue]
 
Zack, don't pay any attention to Skip. He is delusional. He just pretends to not know things.

Gerry
 
Um, Gerry -
I know fields DO work well, and sometimes better - that is precisely why I prefer VBA for a lot of things

If that's the case, I think you need help!

Cheers

[MS MVP - Word]
 
Huh? I don't get it. My point was that yes fields work, and sometimes work better, but really...in this circumstance, is not using a simple VBA function a lot easier for most people than all those field sets? My point was the solution given for doing it with fields is precisely why I would NOT do it with fields. It is my experience that most times, it is easier with VBA. And in THIS case, most definitely is. Especially as there was a requirement to have an update on opening, for which you need VBA anyway.

So...I am not sure what help you mean.

Gerry
 
Hi Gerry,

I have no problem with a vba solution, and yours is fine - it's just the quoted text that seems contradictory. Read it again & I'm sure you'll see what I mean.

Whether a vba solution is 'better' depends on the user's requirements. For some situations, macros and their security issues might be thought less desirable than an admittedly more complex field-based solution. If it's sufficient for the field to update on printing, then having the 'update fields' box checked in Tools|Options|Print would work just as well with the field-based solution.

Cheers

[MS MVP - Word]
 
Yeah...OK. I was trying to be overly polite. As for "sufficient" for the field to be updated on printing - printing was never mentioned.

And as for security issues in a field vs VBA decision, I can not think of any real situation where this is an issue. I mean other than a security situation where VBA is not to be considered AT ALL.

Other than that, frankly, give me a clearly readable function in VBA over any field, any day. As far as I am concerned there is no real performance hit using VBA over fields - so performance is not a factor.

And writing logic in VBA is so much easier than with fields. Complex multiple IF statements with fields are, IMHO, a PITA. Give me a nice Select Case, properly indented.

I hardly ever use fields anymore. Used to. The only one I still use consistently (slight pun intended) is Seq. OK, that is not true if you count Merge fields. Yeah, and some document property stuff for footers.

But for the situation of this post...sheeesh...do all that field stuff when a simple function will do it...no way.

Gerry
 
Thanks very much people.

Skip: I've seen Gerry's handi-work for quite a while, here and at VBAX. He, and a few others (i.e. Tony Jollans, macropod, etc), have always impressed me with their high level of skill and knowledge depth when it comes to Word and the Office Suite.

Fumei: Thank you for your time here, Gerry. I did use your VBA solution in the end. I appreciate your time, knowledge and expertise.

macropod: Thank you very much for your detailed solution. I'm sorry I'm not too good at Word and wasn't really sure how to use your Field solution, though I have no doubt that it would have worked if my preference had not been VBA.

I'm big on VBA. I am also an Excel user. I'm forced to use Access, Word, Publisher, Outlook, etc; I don't want to use them, but I have to. I know the Excel Object Model rather well, so I'm more comfortable in the VBEIDE and programming. That is probably why I went with the VBA solution before the Field solution.

Again, a big thanks to everyone. Much, much appreciated. It would have taken me much longer to figure out that syntax from the Object Model had I not had this valuable resource. :)

Take care!

-----------
Regards,
Zack Barresse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top