Excel is my forte, not Word, so I need help, please. Doing a project for a
non-profit group. Here is the pseudo-code for which a subroutine is needed:
Ask for starting date
Place this value in a field in the document
for j = 1 to 52
print document
add 7 days to date in the field
next j
Also, How do I insert the field?
Would like to format date as: 4 Jan 2009
Many thanks
Hi Bernard,
Don't insert the field with code. Instead, insert the following field in the
base document (press Ctrl+F9 to insert the field braces, and type the rest
inside):
{ DOCVARIABLE docdate }
At first, when you update the field it will say "Error! No document variable
supplied." The macro will fix that by assigning a value to the docdate variable
and then updating the document's fields.
The macro is a bit more than you asked for because VBA will accept various
strings and turn them into dates, but not always the ones the user will expect.
So I built in some error checking and a confirmation dialog to make sure it's OK
to spit out 52 copies.
Sub PrintWeeklyDocs()
Dim sDate As String
Dim dDate As Date
Dim nWeek As Long
Dim rsp As VbMsgBoxResult
Const dtFormat = "d MMM yyyy"
restart:
sDate = InputBox("Enter starting date:")
If Len(Trim(sDate)) = 0 Then Exit Sub ' canceled
On Error Resume Next
dDate = CDate(sDate)
If Err.Number <> 0 Then
MsgBox sDate & " is not a valid date -- try again"
GoTo restart
End If
sDate = Format(dDate, dtFormat)
rsp = MsgBox(prompt:="Confirm " & sDate & "?", buttons:=vbYesNo)
If rsp = vbNo Then GoTo restart
For nWeek = 1 To 52
With ActiveDocument
.Variables("docdate").Value = sDate
.Fields.Update
.PrintOut Background:=False
End With
dDate = DateAdd("d", 7, dDate)
sDate = Format(dDate, dtFormat)
Next
End Sub