Discussion:
Set time for calendar start/end times via VBA
(too old to reply)
OutdoorRuss
2010-01-05 21:04:00 UTC
Permalink
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!
OutdoorRuss
2010-01-05 21:53:33 UTC
Permalink
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?
Sue Mosher [MVP]
2010-01-06 01:07:27 UTC
Permalink
Show your code, please.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by OutdoorRuss
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?
OutdoorRuss
2010-01-06 18:20:43 UTC
Permalink
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.
Sue Mosher [MVP]
2010-01-06 18:34:21 UTC
Permalink
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.outlookcode.com/article.aspx?id=54
Post by OutdoorRuss
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.
OutdoorRuss
2010-01-06 21:52:23 UTC
Permalink
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.
OutdoorRuss
2010-01-06 22:01:11 UTC
Permalink
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.
Sue Mosher [MVP]
2010-01-07 00:04:34 UTC
Permalink
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.outlookcode.com/codedetail.aspx?id=616
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by OutdoorRuss
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.
OutdoorRuss
2010-01-07 15:40:22 UTC
Permalink
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
Sue Mosher [MVP]
2010-01-07 16:04:13 UTC
Permalink
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.outlookcode.com/article.aspx?id=54
Post by OutdoorRuss
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
OutdoorRuss
2010-01-07 16:41:58 UTC
Permalink
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.
Sue Mosher [MVP]
2010-01-07 17:08:20 UTC
Permalink
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.outlookcode.com/article.aspx?id=54
Post by OutdoorRuss
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.
OutdoorRuss
2010-01-06 21:53:14 UTC
Permalink
Duh, I had to put it in as "07:00PM"... works now!
Loading...