Set time for calendar start/end times via VBA

Set time for calendar start/end times via VBA

Post by OutdoorRus » Thu, 07 Jan 2010 06:04:00


I use a custom form to enter certain appointments in at the same time
everyday (12:00pm start to 1:00pm end). I haven't found a way to set
and save these as the default times for the custom form.

If this isn't possible, is there a way to use a macro/VBA to set the
value of the start and end time fields, maybe even when the form
opens?

Thanks!
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Thu, 07 Jan 2010 06:53:33

I've figured out how to set the time fields. However, now my dates
default back to the end of December 1999. How can I set the date to
the date I've clicked on like it does when you click on a date to set
an appointment for that date?

 
 
 

Set time for calendar start/end times via VBA

Post by Sue Mosher » Thu, 07 Jan 2010 10:07:27

Show your code, please.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.yqcomputer.com/
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Fri, 08 Jan 2010 03:20:43

Sub OpenLunchCalendarApptForm()

Set MyFolder = Session.Folders("Volunteers Folder").Folders
("Calendar").Folders("MA Meals")
Set MyItem = MyFolder.Items.Add
("IPM.Appointment.MichiganAve.Meals.Lunch")
MyItem.Display

MyItem.Start = SelectedDate + TimeValue("12:00pm")
MyItem.End = DateAdd("H", 1, MyItem.Start)

End Sub

When I run this, I get the times right, but the date is 12/31/99.
 
 
 

Set time for calendar start/end times via VBA

Post by Sue Mosher » Fri, 08 Jan 2010 03:34:21

For troubleshooting purposes, I would suggest that you build the date/time
value separately and check what you actually get, e.g.

MsgBox SelectedDate
theDate = SelectedDate + TimeValue("12:00pm")
MsgBox theDate
MyItem.Start = theDate

We have no way of knowing, of course, how you're separately setting the
value of what apparently is a global variable, SelectedDate.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.yqcomputer.com/
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Fri, 08 Jan 2010 06:52:23

Sue,

I had luck with something like this:

Function Item_Open ()

Dim SelCalendarDate
SelCalendarDate = DateValue(Item.Start)

Item.Start = SelCalendarDate & " " & TimeValue("12:00pm")
Item.End = DateAdd("H", 1, Item.Start)


End Function
___

So, basically it grabs the original calendar date when the form opens
and then reinserts it as part of the string. It worked well in this
case. However, when I changed the time to "7:00PM" it came back with
9am again.
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Fri, 08 Jan 2010 06:53:14

Duh, I had to put it in as "07:00PM"... works now!
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Fri, 08 Jan 2010 07:01:11

Is there a code/item for the date that is selected on the calendar?
The code above works great for the default custom form (if I, for
instance, double-click on a date and it opens the default form, etc).
But, if I try to open a form with a macro, then it defaults the form
to today's date instead of the date that is selected/highlighted on
the calendar. For instance, today is the 6th, but I want an
appointment for the 10th. So, I click on the 10th and then click my
toolbar macro button to open my custom form. However, it opens to
today's (6th) date and not the 10th as I have selected. I wonder if
there's a way for it to open with the selected date.
 
 
 

Set time for calendar start/end times via VBA

Post by Sue Mosher » Fri, 08 Jan 2010 09:04:34

No, but you can use a CommandBars technique to create a new item and read
the date/time values off that item; see
http://www.yqcomputer.com/

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.yqcomputer.com/
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Sat, 09 Jan 2010 00:40:22

Sue,

Thank you so much. This is exactly what I need!

Where would I put the "objAppt.Close olDiscard" in the code to close
the non-custom appointment? When I put it at the end, it closes the
custom appointment instead.

Russ
 
 
 

Set time for calendar start/end times via VBA

Post by Sue Mosher » Sat, 09 Jan 2010 01:04:13

Sorry, but I don't have enough context to answer that question. You need to
call the Close method on the appointment you want to discard, at the point
in your code when you are finished using it.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.yqcomputer.com/
 
 
 

Set time for calendar start/end times via VBA

Post by OutdoorRus » Sat, 09 Jan 2010 01:41:58

Sue,

I was referring to the code you suggested. In the comments you
mention adding that line in the code but don't mention where it would
go in the code.
 
 
 

Set time for calendar start/end times via VBA

Post by Sue Mosher » Sat, 09 Jan 2010 02:08:20

As I said, it goes after any code that uses the item you want to close.

It would be helpful if you quoted enough of the previous discussion so those
of us using offline newsreaders can follow it.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.yqcomputer.com/