PRINTDATE Field Calculation

W

Word_2007_SASS

Hi All,

We currently have a Word 2007 template in which we require two dynamic dates
to appear on the cover page. The first date "Approval Date" is a typical
PRINTDATE field, but the second field is a "Review Date" and needs to be
equal to the PRINTDATE plus 3 years.

How can we write code in vba to insert a PRINTDATE field and calculate that
field to be equal to 3 years from the print date?

Any help is appreciated!

Thanks!
 
G

Graham Mayor

If you only want to add three full years to the field, the construction of
the fields would be

{ PrintDate \@ "d MMM" } { ={ PrintDate \@ "yyyy" } + 3 }

However if you want to use vba, you may as well intercept the print routine
and add a couple of docvariable fields ( {DocVariable varPrintDate} and
{DocVariable varNextPrint } ) to the places you want the two dates to appear
and the following macro will update those fields when you print the
document.


Sub FilePrint()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varPrintDate").Value = "Approval Date:" & vbTab & Format(Date, "d MMM
yyyy")
oVars("varNextPrint").Value = "Review Date:" & vbTab & Format(Date, "d MMM
") & Format(Date, "yyyy") + 3
ActiveDocument.Fields.Update
ActiveDocument.PrintOut
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

Word_2007_SASS

Sorry, I should have emphasized that the Review Date MUST be a field, so that
if the Approval Date updates when the document is printed, the Review Date
must also update so that the dates match and the Review Date is 3 years in
the future.

Thanks!
 
G

Graham Mayor

Both my suggestions use fields?
In the case of the macro suggestion, this has the additional advantage of
updating the field before the document is printed so you may immediately see
the change in the document, by intercepting the print command and updating
docvariable fields in the document.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter Jamieson

{ PrintDate \@ "d MMM" } { ={ PrintDate \@ "yyyy" } + 3 }

If you want it to work on 29 Feb I think you have to do a bit more, e.g.

{ QUOTE "{ ={ PRINTDATE \@YYYY }+3 }{ IF { PRINTDATE \@MDD } = 229
"-02-28" "{ PRINTDATE \@-MM-DD }" } \@DD MMM YYYY }

Same kind of problem for the VBA approach.

Peter Jamieson

http://tips.pjmsn.me.uk
 
G

Graham Mayor

Good thinking - the modified code for the macro version could be

Sub FilePrint()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varPrintDate").Value = "Approval Date:" & vbTab & _
Format(Date, "d MMM yyyy")
If Month(DateSerial(Format(Date, "yyyy"), 2, 29)) = 2 And _
Day(Date) = 29 Then
oVars("varNextPrint").Value = "Review Date:" & vbTab & _
"1 Mar " & Format(Date, "yyyy") + 3
Else
oVars("varNextPrint").Value = "Review Date:" & vbTab & _
Format(Date, "d MMM ") & Format(Date, "yyyy") + 3
End If
ActiveDocument.Fields.Update
ActiveDocument.PrintOut
End SubEnd Sub

which will set the Review Date to the 1st March on the 29th Deb of a leap
year.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

Word_2007_SASS

Perfect! Thanks for all the help :)



Graham Mayor said:
Good thinking - the modified code for the macro version could be

Sub FilePrint()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varPrintDate").Value = "Approval Date:" & vbTab & _
Format(Date, "d MMM yyyy")
If Month(DateSerial(Format(Date, "yyyy"), 2, 29)) = 2 And _
Day(Date) = 29 Then
oVars("varNextPrint").Value = "Review Date:" & vbTab & _
"1 Mar " & Format(Date, "yyyy") + 3
Else
oVars("varNextPrint").Value = "Review Date:" & vbTab & _
Format(Date, "d MMM ") & Format(Date, "yyyy") + 3
End If
ActiveDocument.Fields.Update
ActiveDocument.PrintOut
End SubEnd Sub

which will set the Review Date to the 1st March on the 29th Deb of a leap
year.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>






.
 
M

macropod

Hi,

To see how to do this and just about everything else you might want to do with dates in Word, check out my Microsoft Word Date
Calculation Tutorial, at:
http://lounge.windowssecrets.com/index.php?showtopic=249902
or
http://www.gmayor.com/downloads.htm#Third_party
In particular, look at the item titled 'Calculate a day, date, month and year, using n years delay'. You'll need to change the
field's DATE variables to PRINTDATE. If you want the ability to interactively calculate the date, see also 'Interactively Calculate
A Past Or Future Date'. Do read the document's introductory material.
 

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