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

Add to or subtract from date and time values in Word 1

Status
Not open for further replies.

shib1148

IS-IT--Management
Sep 21, 2005
18
US
Hello All,

Does anyone know how to make it so that a date 15 days from current date can be inserted into a MS Word Document?
 
Hi shib1148,

This can be done with a field - but it is very complex. A better way, generally, is with VBA - a simple routine can do the calculation and insert it at, say, the cursor position:
Code:
Selection.InsertAfter Now + 15

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
I would be willing to work with the Field option as I am unfamiliar with VBA. Would you happen to have a link to how to do this with a Field??

Thank in advance.
 
I agree with Tony that using VBA for this is easiest. Then you can put it just about anywhere you want.

In a bookmark:
Code:
ActiveDocument.Bookmarks("Later").Range.Text = _
Format(Now + 15, "dddd, mmm d yyyy")
In a formfield:
Code:
ActiveDocument.Formfields("Later").Result = _
Format(Now + 15, "dddd, mmm d yyyy")
In, of course, any date format you want it to be...

Gerry
 
If you want a field-based solution, this should work (all braces to be entered by Ctrl+F9):

[blue][tt]{ Quote { Set Year { Date \@ yyyy } }
{ Set Leap { =(MOD(Year,4)=0) - (MOD(Year,100)=0) + (MOD(Year,400)=0) } }
{ Set Month { Date \@ M } }
{ Set EoMonth { =IF(Month=2, 28+Leap, 31-MAX(Month=4,Month=6,Month=9,Month=11)) } }
{ Set Day { ={ Date \@ dd } + 15 } }
{ Set Month { =Month + (Day>EoMonth) } }
{ Set Day { =IF(Day>EoMonth, Day-EoMonth, Day) } }
{ Set Year { =Year + (Month=13) } }
{ Set Month { =IF(Month=13, 1, Month) } }
{ Set NewDate { =(Year*10000) + (Month*100) + Day } }
{ ={ NewDate } \# "0000/00/00" } \@ "dd MMMM yyyy" }
[/tt][/blue]

Member macropod has a document dedicated to this kind of stuff somewhere but I don't have a url to hand (it's on Woody's Lounge, I think the website is called) - and he has a solution for any number of days - the one above only works up to 28 days.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
The VBA is very neat. I'd probably have kluged it by putting =TODAY()+15 in an Excel cell and pasting a link in the doc!

 
Thank you all.

the Field option worked very well.
 
Lilliabeth.....talk about the long way around! [atom]

Gerry
 
Hi folks,

I've been away a while. For just about everything you might want to do with dates *and times* in Word using field coding, my Date Calc 'tutorial' is at:

Cheers
PS: Chris Woodman's macro creates code that only works up to 28 days. My field coding will handle pretty well any date calculations you'd need, plus a few orders of magnitude more!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top