Macro to Calculate a future date

M

Mark

I am trying to create a macro in Word (I have never done before) to calculate
a future date i.e., 90 days from the current date.

I found some information on line for the calculation command line but when I
run the macro, it inserts the command line where the future date is. What am
I doing wrong?
 
M

macropod

Hi Mark,

At its simplest:
Sub FutureDate()
MsgBox Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
The format function is needed to strip off the time portion of the calculation.

If you want that date to go into a document, you'll need to specify the location. For example:
Sub FutureDate()
Selection.TypeText Text:=Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub

or, to insert the text at a nominated location without having to select it:
Sub FutureDate()
ActiveDocument.Bookmarks("BkMrk").Range.Text = Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
Where 'BkMrk' is the name of a bookmark.

To see how to do this and just about everything else you might want to do with dates in Word using field coding instead of vba,
check out my Date Calc 'tutorial', at:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
or
http://www.gmayor.com/downloads.htm#Third_party
Do read the document's introductory material.
 
M

Mark

Thanks for the information. Your documentation is referenced all over the
internet!

This is a bit over my head. I have never knowingly used or created macros or
programed fields before. Can you posibably email me a document with the macro
in it or filed example, such as your example of a "The DATE" field in your
instructions entitled WORD Date & Time Manipulation: Tips & Techniques? If
you can use the example of 90 days into the future I would REALLY appreciate
it. My email address is (e-mail address removed).

macropod said:
Hi Mark,

At its simplest:
Sub FutureDate()
MsgBox Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
The format function is needed to strip off the time portion of the calculation.

If you want that date to go into a document, you'll need to specify the location. For example:
Sub FutureDate()
Selection.TypeText Text:=Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub

or, to insert the text at a nominated location without having to select it:
Sub FutureDate()
ActiveDocument.Bookmarks("BkMrk").Range.Text = Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
Where 'BkMrk' is the name of a bookmark.

To see how to do this and just about everything else you might want to do with dates in Word using field coding instead of vba,
check out my Date Calc 'tutorial', at:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
or
http://www.gmayor.com/downloads.htm#Third_party
Do read the document's introductory material.

--
Cheers
macropod
[MVP - Microsoft Word]


Mark said:
I am trying to create a macro in Word (I have never done before) to calculate
a future date i.e., 90 days from the current date.

I found some information on line for the calculation command line but when I
run the macro, it inserts the command line where the future date is. What am
I doing wrong?
 
J

Jonathan West

Mark said:
Thanks for the information. Your documentation is referenced all over the
internet!

This is a bit over my head. I have never knowingly used or created macros
or
programed fields before. Can you posibably email me a document with the
macro
in it or filed example, such as your example of a "The DATE" field in your
instructions entitled WORD Date & Time Manipulation: Tips & Techniques? If
you can use the example of 90 days into the future I would REALLY
appreciate
it. My email address is (e-mail address removed).


Hi Mark

This article might be useful to you


What do I do with macros sent to me by other newsgroup readers to help me
out?
http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
 
M

macropod

Hi Mark,

The zip file in either of the links I gave you contains the document, with all the field coding in place. All you need to do is to
copy the appropriate field code (probably from the example titled 'Calculate a day, date, month and year, using n days delay') into
your document and adjust to suit your requirements. Instructions on how to modify the fields are contained in the 'Introduction'.
Apart from possible changes to suit your regional location, you might want to change the displayed date format and, of course,
change the 'Delay' value from 14 to 90. Depending on how you're using the field, you might also want to change the 'DATE' strings to
'CREATEDATE', 'SAVEDATE' or 'PRINTDATE'.

--
Cheers
macropod
[MVP - Microsoft Word]


Mark said:
Thanks for the information. Your documentation is referenced all over the
internet!

This is a bit over my head. I have never knowingly used or created macros or
programed fields before. Can you posibably email me a document with the macro
in it or filed example, such as your example of a "The DATE" field in your
instructions entitled WORD Date & Time Manipulation: Tips & Techniques? If
you can use the example of 90 days into the future I would REALLY appreciate
it. My email address is (e-mail address removed).

macropod said:
Hi Mark,

At its simplest:
Sub FutureDate()
MsgBox Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
The format function is needed to strip off the time portion of the calculation.

If you want that date to go into a document, you'll need to specify the location. For example:
Sub FutureDate()
Selection.TypeText Text:=Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub

or, to insert the text at a nominated location without having to select it:
Sub FutureDate()
ActiveDocument.Bookmarks("BkMrk").Range.Text = Format(DateAdd("d", 90, Now), "d MMMM yyyy")
End Sub
Where 'BkMrk' is the name of a bookmark.

To see how to do this and just about everything else you might want to do with dates in Word using field coding instead of vba,
check out my Date Calc 'tutorial', at:
http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Number=249902
or
http://www.gmayor.com/downloads.htm#Third_party
Do read the document's introductory material.

--
Cheers
macropod
[MVP - Microsoft Word]


Mark said:
I am trying to create a macro in Word (I have never done before) to calculate
a future date i.e., 90 days from the current date.

I found some information on line for the calculation command line but when I
run the macro, it inserts the command line where the future date is. What am
I doing wrong?
 

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