Welcome toWord.Tips.Net
Ask a Word Question
Make a Comment
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Collapsing and Expanding Subdocuments
For some documents, it is helpful to have a list of dates that you can use as the basis for your work. For instance, you may have to create a report that lists all the dates between now and the end of the year, along with a person's name or a project name to the right of the date. The starting point, of course, is getting the list of dates.
There are a couple of ways you can approach generating the list. One easy method is to use Excel in conjunction with Word. Excel's AutoFill feature makes generating a list of dates amazingly easy. Once you have the list in Excel, you can then copy and paste it into the Word document, or you can use mail merge to merge the dates into the document (if that approach is appropriate for your needs).
If you prefer to not use Excel for some reason, the best solution is to use a macro. The following macro very quickly creates a list of all the dates for the year 2009:
Sub PrintYearDays()
Dim DateToday As Date
Dim T As Integer
'Because the date is going to be changed, save it
DateToday = Date
Date = #12/31/2008#
For T = 1 To 365
Selection.TypeText Text:=Format(Date + T, _
"mmmm dd yyyy")
Selection.TypeParagraph
Next T
'Restore today's date
Date = DateToday
End Sub
Notice that the macro works by resetting the date on your system. Today's current date is stored in the DateToday variable, and then the date is reset to the starting date for your range. If you want to have the macro work for some other date range, just change the starting date, along with the ending value of the For ... Next loop.
If you need to create date lists, and you never quite know what the beginning and ending dates in the range will be, then a different macro approach makes more sense. The following macro asks you for both a beginning and ending date:
Sub ListDates()
Dim ListDate as Date
Dim StartDate As Date
Dim EndDate As Date
Dim Repeats As Integer
'Gets user input
StartDate = InputBox("Please enter the starting date.", _
"Start Date", "Start Date")
EndDate = InputBox("Please enter the ending date.", _
"End Date", "End Date")
'Enters the start date in the document
Selection.TypeText Text:=Format(StartDate, _
"dddd, MMMM dd, yyyy")
Selection.TypeText (vbCr & vbLf)
'Determines the number of dates to print
Repeats = DateDiff("d", StartDate, EndDate)
'Loops to print the list of dates
For i = 1 To Repeats
ListDate = DateAdd("d", i, StartDate)
Selection.TypeText Text:=Format(ListDate, _
"dddd, MMMM dd, yyyy")
Selection.TypeParagraph
Next i
End Sub
The StartDate and EndDate variables, set by your input, determines how many times the For ... Next loop is repeated.
Tip #3864 applies to Microsoft Word versions: 97 2000 2002 2003 2007
Take Control! Master the real power behind Word! Successfully master the secrets of powerful formatting and create documents that stand out from the rest. Best of all, you can create documents that are easy to maintain and quick to change.