How do I enter a field with the current time + six hours?

R

roursler

I am making a macro for printing medication labels. I need to have an
expiration time on the label and would like it to be the current time + 6
hours. How can I structure the field to do this?
 
D

Doug Robbins - Word MVP

I would put a { docvariable exptime } field in the label where you want the
time to appear and in your code use

With ActiveDocument
.Variables("exptime").Value = Format(DateAdd("h", 6, Now), "h:mm am/pm")
.Range.Fields.Update
.PrintOut
End With

You must use Ctrl+F9 to insert the field delimiters around the docvariable
field and use Alt+F9 to toggle off their display

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
K

Karl E. Peterson

Doug Robbins - Word MVP explained on 1/15/2010 :
Format(DateAdd("h", 6, Now), "h:mm am/pm")

Not that there's anything wrong with that approach. It's probably the
most readable answer there is. Just wanted to kick in another method,
for sake of having all options out there.

Background: Date variables are really Doubles in drag. The day is
stored in the whole portion, and the time in the fractional part.

So, to add 6 hours, one need only add a quarter of a day:

Format(Now + 0.25, "h:mm am/pm")

Might be useful, for more calculation intensive operations...
 
D

Doug Robbins - Word MVP

Thanks, Karl. Good to know.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
R

redtwotwo

Hi. I am trying something similar as this but I would like to create a
sheet of labels which has the date + 15 days. How would you suggest
implementing this? There are many labels per page, do I have to create
many different variables and allow the vba to set each one or is there
an easier way?

I have used vba in excel but this is my first venture for word. I
would be grateful for help in this area.

Thanks
Heather

Thanks, Karl.  Good to know.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Karl E. Peterson said:
Doug Robbins - Word MVP explained on 1/15/2010 :
Not that there's anything wrong with that approach.  It's probably the
most readable answer there is.  Just wanted to kick in another method, for
sake of having all options out there.
Background: Date variables are really Doubles in drag.  The day is stored
in the whole portion, and the time in the fractional part.
So, to add 6 hours, one need only add a quarter of a day:
   Format(Now + 0.25, "h:mm am/pm")
Might be useful, for more calculation intensive operations...
 
G

Graham Mayor

See http://www.gmayor.com/insert_a_date_other_than_today.htm and in
particular the link to http://www.gmayor.com/downloads.htm#Third_party

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Hi. I am trying something similar as this but I would like to create a
sheet of labels which has the date + 15 days. How would you suggest
implementing this? There are many labels per page, do I have to create
many different variables and allow the vba to set each one or is there
an easier way?

I have used vba in excel but this is my first venture for word. I
would be grateful for help in this area.

Thanks
Heather

Thanks, Karl. Good to know.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Doug Robbins - Word MVP explained on 1/15/2010 :
Not that there's anything wrong with that approach. It's probably the
most readable answer there is. Just wanted to kick in another method,
for
sake of having all options out there.
Background: Date variables are really Doubles in drag. The day is stored
in the whole portion, and the time in the fractional part.
So, to add 6 hours, one need only add a quarter of a day:
Format(Now + 0.25, "h:mm am/pm")
Might be useful, for more calculation intensive operations...
 
D

Doug Robbins - Word MVP

In the Labels dialog in word, having selected the type of label that you are
using and with the selection in the Address area of the dialog, Press
Ctrl+F9 to insert a pair of field delimiters { } and inside them type

{ DOCVARIABLE varDate }

The select the Full page of the same label radio button and then click on
New Document.

Then with that document as the active document, run a macro containing the
following code

With ActiveDocument
.Variables("varDate").Value = Format(DateAdd("d", 15, Now), "dd mmmm
yyyy" )
.Range.Fields.Update
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

redtwotwo said:
Hi. I am trying something similar as this but I would like to create a
sheet of labels which has the date + 15 days. How would you suggest
implementing this? There are many labels per page, do I have to create
many different variables and allow the vba to set each one or is there
an easier way?

I have used vba in excel but this is my first venture for word. I
would be grateful for help in this area.

Thanks
Heather

Thanks, Karl. Good to know.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Doug Robbins - Word MVP explained on 1/15/2010 :
would like it to be the current time + 6 hours.
Format(DateAdd("h", 6, Now), "h:mm am/pm")
Not that there's anything wrong with that approach. It's probably the
most readable answer there is. Just wanted to kick in another method,
for
sake of having all options out there.
Background: Date variables are really Doubles in drag. The day is
stored
in the whole portion, and the time in the fractional part.
So, to add 6 hours, one need only add a quarter of a day:
Format(Now + 0.25, "h:mm am/pm")
Might be useful, for more calculation intensive operations...
 

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