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

MS Word Date Automation 1

Status
Not open for further replies.

M3Fan

Programmer
Dec 28, 2001
73
US
Ok, here's what I need to do: I want to enter an initial date on say the first column in a 5-column wide table in an MS word document, and then I want Word to automatically populate the next 4 cells with the successive 4 day's dates afterwords. For example

| First date entered here,5/20/03 | 5/21/03 | 5/22/03 | 5/23/03 | 5/24/03 |

This is a template used over and over and it's a pain to type all 5 dates in every week. I'm pretty knowledgable in VB, but haven't used wordbasic. Where do I start?
 
This code will work. Add it to AutoNew() (to run when a new document is created from the template) or add it to AutoOpen() to run whenever a document created from the template is opened.

Sub AutoNew()

Dim i As Integer

'You need to add bookmark to the template, called today
'marking text in the first column of the table.
'This code will move to the bookmark, delete the holder
'text (if any) and type add the current date in the
'cell.

ActiveDocument.Bookmarks("Today").Select
Selection.Delete
Selection.TypeText Date

'This code moves to the next cell, adds the date for i
'days in the future.
For i = 1 To 4
Selection.Move wdCell
Selection.TypeText DateAdd("d", i, Date)
Next i

End Sub

Note that 4 could be changed to a variable if the number of
days to add is variable.

rwgress
 
This was VERY HELPFUL, and worked nicely. What if I want to prompt the user for the starting date, rather than taking today's date by default?
 
Whoops, figured this out myself by just guessing a msgbox would work- that did it. The only change I made was creating a new variable called newdate and setting newdate = msgbox("Please enter starting date"). This worked out really well, thanks again.
 
... Substitute InputBox for Msgbox in the above post ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top