如何用VBA在Outlook选定日期添加7:00-9:30的外出状态约会?
Solution: Create Fixed-Time Appointment on Selected Date with Out-of-Office Status
Absolutely doable! Your existing code relies on the selected time range from the calendar view, but we can easily tweak it to lock in the 7:00–9:30 window on whichever date you’ve selected, plus set the appointment status to "Out of Office". Here’s the modified code and a breakdown of the key changes:
Modified VBA Code
Sub CreateFixedTimeOOOAppointment() Dim datStart As Date Dim datEnd As Date Dim oView As Outlook.View Dim oCalView As Outlook.CalendarView Dim oExpl As Outlook.Explorer Dim oFolder As Outlook.Folder Dim oAppt As Outlook.AppointmentItem Const datNull As Date = #1/1/4501# Set oExpl = Application.ActiveExplorer Set oFolder = oExpl.CurrentFolder Set oView = oExpl.CurrentView ' Verify we're working in a calendar view If oView.ViewType = olCalendarView Then Set oCalView = oView ' Extract just the date from the selected time (ignore any time component) Dim selectedDate As Date selectedDate = DateValue(oCalView.SelectedStartTime) ' Set fixed start (7:00 AM) and end (9:30 AM) times datStart = selectedDate + TimeSerial(7, 0, 0) datEnd = selectedDate + TimeSerial(9, 30, 0) ' Create the new appointment Set oAppt = oFolder.Items.Add("IPM.Appointment") If selectedDate <> datNull Then oAppt.Start = datStart oAppt.End = datEnd ' Mark the appointment as "Out of Office" oAppt.BusyStatus = olOutOfOffice ' Optional: Add a clear subject for quick recognition oAppt.Subject = "Out of Office (7:00–9:30)" End If oAppt.Display End If End Sub
Key Changes Explained
- Extract the selected date: Using
DateValue()strips the time component fromSelectedStartTime, so no matter which time slot you click on the calendar, we only use the underlying date. - Fixed time window:
TimeSerial(7, 0, 0)creates a 7:00 AM time value that we add to the selected date to get the exact start time. We use the same logic for 9:30 AM withTimeSerial(9, 30, 0). - Out-of-Office status: The
BusyStatusproperty uses Outlook’s built-inolOutOfOfficeconstant (value = 3) to set the appointment’s status to "Out of Office". - Optional subject: Added a default subject to make the appointment’s purpose clear, but you can modify or remove this line as needed.
How to Use
- Open Outlook and switch to your Calendar view.
- Click on any date (or time slot on that date) where you want to add the appointment.
- Run the macro—you’ll see the appointment pop up with the correct time and status ready to save.
内容的提问来源于stack exchange,提问作者Andy M
相关产品推荐
相关产品推荐

